2

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.

Community
  • 1
  • 1
Rocky
  • 4,454
  • 14
  • 64
  • 119

2 Answers2

4

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
        }
    }
}
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
  • Or do the redirect (or whatever) *instead of* the call to 'function_redirect'. – Hans Kesting Feb 03 '12 at 10:19
  • in the function "function_redirect" how could I know which actionlink is clicked or user click on which btn because every thing is dynamic. but when i am redirecting page explicitly then its working fine – Rocky Feb 03 '12 at 12:43
0

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
})
Yorgo
  • 2,668
  • 1
  • 16
  • 24