单击“确认”对话框按钮时,如何返回true或false
How may I return true or false when I click on confirm dialog button?
我有一段jQuery
$(function () {
$('.checked').click(function (e) {
e.preventDefault();
var dirtyvalue = "Test1";
var textvalue= "Test";
if (dirtyvalue.toLowerCase() != textvalue.toLowerCase()) {
{
var dialog = $('<p>Do you want to save your changes?</p>').dialog({
buttons: {
"Yes": function () {
dialog.dialog('close');
alert('yes btn called');
},
"No": function () {
dialog.dialog('close');
alert('No btn called');
},
"Cancel": function () {
dialog.dialog('close');
}
}
});
}
return false;
}
return true;
});
});
我想在点击按钮时返回true或false;'"Yes"应返回true,"No"应返回true,"Cancel"应返回false。
我已经检查了这个链接,但这并不能解决我的问题。另请参阅我之前的问题。
我的场景是,我有许多ActionLink和TreeView链接,它们导航到各自的页面。假设我在第1页上有这个JS,并且在该页上有许多操作链接。点击第2页,此时我的确认应该打开,当我点击"否"按钮时,它应该重定向到第2页。
对话框不能返回值,必须将要执行的操作放入另一个函数中。例如
var dialog = $('<p>Are you sure?</p>').dialog({
buttons: {
"Yes": function()
{
alert('you chose yes');
//call a function that redirects you
function_redirect(true);
},
"No": function()
{
alert('you chose no');
//call a function that redirects you
function_redirect(true);
},
"Cancel": function()
{
alert('you chose cancel');
//just close the dialog
dialog.dialog('close');
}
}
});
var function_redirect = function(redirect){
if(redirect === true){
//redirect to page2
}
}
你应该把你需要实现的逻辑放在"function_redirect
"(或你想要的任何函数)中,并根据按下的按钮传递参数
编辑-如果你需要知道点击了什么按钮,只需使用传递给函数的事件对象
"Yes": function(e) {
//e.target is the element of the dom that has been clicked
//from e.target you can understand where you are and act accordingly
//or pass it to function_redirect
function_redirect(true, e.target);
var function_redirect = function(redirect, target){
if(redirect === true){
if($(target).parent().attr('id') === 'page2'){
//redirect to page2
}else{
//do something else
}
}
}
对话框异步工作。所以你不能返回任何值。您应该使用回调函数。
$("a").click(dialog.dialog("open"),function(data){
//do your redirect here
})
相关文章:
- 对于loop.if-仅在经过所有间隔后返回true
- 如果函数返回True,则显示Javascript按钮
- Javascript If else 只返回 TRUE 或只返回 FALSE
- 过滤器返回true或false
- 我对“;返回true"嵌套函数内部;t工作
- 如何在成功输入时使“返回TRUE”
- 检查一个元素是否有一个类与另一个类总是返回true
- JavaScript函数未返回true
- 如果URL中有任何字符串变体匹配,则返回true
- 如何使以下函数返回true或false
- 使用“in”关键字检查 0 始终返回 true
- hasOwnProperty 在对照父对象属性进行检查时返回 true
- 为什么 isNaN 总是返回 true
- _.数组中的每个查找值都返回 true 或 false.使用下划线 JS
- 错误地返回true
- 为什么asp:LinkButton服务器端事件没有'当clientClick返回true时,t激发
- 阻止表单提交 javascript 为什么有一个明显的错误语句返回 true
- Node js命令器args返回true而不是值
- 如何将html表单与php链接,以便在例如单击按钮时返回true
- 从OnClientClick调用函数时,在JavaScript中返回true