我有一段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; '是'应该返回true,'否'应该返回true,'Cancel'应该返回false。
我已经检查了这个link,但这并没有解决我的问题。 另见我之前的question。
我的方案是我有很多ActionLink和TreeView链接导航到他们各自的页面。假设我在第1页,我有这个JS,在那个页面上我有很多动作链接。点击第2页,那时我的确认应该打开,当我点击“否”按钮时,它应该重定向到第2页。
答案 0 :(得分:4)
对话框无法返回值,您必须在另一个函数中放置您想要执行的操作。例如
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
}
}
}
答案 1 :(得分:0)
Dialog工作异步。所以你不能回报任何价值。你应该使用回调函数。
$("a").click(dialog.dialog("open"),function(data){
//do your redirect here
})