jquery ui dialog box need to return value, when user presses button, but not working

前端 未结 5 1235
面向向阳花
面向向阳花 2020-11-29 09:32

I\'v got a jquery ui dialog box I want to use to prompt the user to confirm a deletion. When the user presses \"yes\" or \"no\" I need to return \"True\" or \"False\" to co

5条回答
  •  情歌与酒
    2020-11-29 09:59

    U should see this answer.

    Well, This can work.

    Your dialog function... showDialog()

    function confirmation(question) {
        var defer = $.Deferred();
        $('
    ') .html(question) .dialog({ autoOpen: true, modal: true, title: 'Confirmation', buttons: { "Yes": function () { $(this).dialog("close"); defer.resolve("true");//this text 'true' can be anything. But for this usage, it should be true or false. }, "No": function () { $(this).dialog("close"); defer.resolve("false");//this text 'false' can be anything. But for this usage, it should be true or false. } }, close: function () { $(this).remove(); } }); return defer.promise(); }

    and then the code where you use the function...

    function onclick(){
        var question = "Do you want to start a war?";
        confirmation(question).then(function (answer) {
    
            if(answer=="true"){
                alert("this is obviously " + ansbool);//TRUE
            } else {
                alert("and then there is " + ansbool);//FALSE
            }
        });
    }
    

    This may seem wrong for most people. But there is always some situations where you just can't go without return from JQuery Dialog.

    This will basically mimic the confirm() function. But with ugly code and a nice confirm box look :)

    I hope this helps some people out.

    Honestly,I was take many time for this, finally i found best solution.you can see more detail here=>Awesome Solution

提交回复
热议问题