【问题标题】:How may I return true or false when I click on confirm dialog button?单击确认对话框按钮时如何返回真或假?
【发布时间】:2012-02-03 09:50:10
【问题描述】:

我有一个 jQuery 的 sn-p

$(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;

    });
});

我想在按钮点击时返回真或假; “是”应该返回真,“否”应该返回真,而“取消”应该返回假。

我已经检查了这个link,但这并不能解决我的问题。 另见我之前的question

我的情况是我有许多 ActionLink 和 TreeView 链接,它们导航到各自的页面。假设我在我有这个 JS 的第 1 页上,并且在那个页面上我有很多动作链接。单击第 2 页,那时我的确认应该打开,当我单击“否”按钮时,它应该重定向到第 2 页。

【问题讨论】:

    标签: javascript jquery asp.net


    【解决方案1】:

    对话框不能返回值,你必须把你想做的事情放在另一个函数中。例如

            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
            }
        }
    }
    

    【讨论】:

    • 或者做重定向(或其他)而不是调用'function_redirect'。
    • 在函数“function_redirect”中我怎么知道点击了哪个actionlink或用户点击了哪个btn,因为每件事都是动态的。但是当我明确重定向页面时,它的工作正常
    【解决方案2】:

    对话框异步工作。所以你不能返回任何值。你应该使用回调函数。

    $("a").click(dialog.dialog("open"),function(data){
         //do your redirect here
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-12
      相关资源
      最近更新 更多