简体   繁体   中英

How may I return true or false when I click on confirm dialog button?

I have a snippet of 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;

    });
});

I want to return true or false on button click; 'Yes' should return true, 'No' should return true, and 'Cancel' should return false.

I already checked this link , but this is not solving my problem. See also my previous question .

My scenario is that I have many ActionLink and TreeView links which navigate to their respective pages. Suppose I am on page 1 where I have this JS, and on that page I have many action links. Clicking on page 2, at that time my confirm should open, and when I click on the button 'No' it should redirect to page 2.

A dialog can't return a value, you must put what you want to do in another function. for example

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

you should put the logic you need to implement in " function_redirect " (or in whatever function you want) and pass parametrs according to the button pressed

EDIT - if you need to know what button has been clicked, just use the event object that is passed to the function

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

Dialog works async. So you cant return any value. You should use callback function.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM