jquery ui对话框需要返回值,当用户按下按钮,但不工作 [英] jquery ui dialog box need to return value, when user presses button, but not working

查看:21
本文介绍了jquery ui对话框需要返回值,当用户按下按钮,但不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 jquery ui 对话框,我想用它来提示用户确认删除.当用户按是"或否"时,我需要返回真"或假"以继续执行一些 javascript.下面代码的问题是当对话框出现时它立即执行return true;"但用户还没有按下是"按钮?

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 continue some javascript execution. The problem with the code below is when the dialog box shows up it immediately is executing a "return true;" but the user hasn't pressed the "Yes" button yet?

我做错了什么?

HTML:

<div id="modal_confirm_yes_no" title="Confirm"></div>

Javascript 调用:

Javascript call:

$("#modal_confirm_yes_no").html("Are you sure you want to delete this?");
var answer = $("#modal_confirm_yes_no").dialog("open");

if (answer)
{
     //delete
}
else
{
     //don't delete
}

Jquery 对话框:

Jquery dialog:

$("#modal_confirm_yes_no").dialog({
                bgiframe: true,
                autoOpen: false,
                minHeight: 200,
                width: 350,
                modal: true,
                closeOnEscape: false,
                draggable: false,
                resizable: false,
                buttons: {
                        'Yes': function(){
                            $(this).dialog('close');
                            return true;
                        },
                        'No': function(){
                            $(this).dialog('close');
                            return false;
                        }
                    }
            });

推荐答案

javascript 是异步的.

javascript is asynchronous.

所以你必须使用回调:

   $("#modal_confirm_yes_no").dialog({
            bgiframe: true,
            autoOpen: false,
            minHeight: 200,
            width: 350,
            modal: true,
            closeOnEscape: false,
            draggable: false,
            resizable: false,
            buttons: {
                    'Yes': function(){
                        $(this).dialog('close');
                        callback(true);
                    },
                    'No': function(){
                        $(this).dialog('close');
                        callback(false);
                    }
                }
        });
    function callback(value){
         //do something
    }

这篇关于jquery ui对话框需要返回值,当用户按下按钮,但不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆