javascript - 确定 jquery 对话框的结果。将对话的结果分配给变量

标签 javascript jquery jquery-ui

在javascript中可以使用对话框返回一个变量,例如:

//sets variable as false
var shouldIngest = false; 
//depends if cancel, ok, or the "x" is clicked
shouldIngest = confirm("Do you want to ingest?");

if( shouldIngest ) {
    //do some things...
}else{
   //do something else...
}

我想使用 jQuery UI dialog所以我可以稍微自定义/设置确认框的样式。我如何使用 jQuery UI 对话框实现相同的目的?我想捕获 var shouldIngest 的对话结果。下面的代码似乎不起作用。

var shouldIngest = false;

$('#ingestConfirmDialog').html('Do you want to Ingest this document into the form?');
shouldIngest = $('#ingestConfirmDialog').dialog({
    modal : true,
    draggable : false,
    buttons : {
        "Yes Ingest this Docx" : function () {
            $(this).dialog("close");    
        },
       "Just Add as Attachment" : function () {
           $(this).dialog("close");
       }
   }

});

我该怎么做才能解决这个问题?提前谢谢你。

最佳答案

只需在按钮回调中设置变量:

buttons : {
    "Yes Ingest this Docx" : function () {
        shouldIngest = true;
        $(this).dialog("close");    
    },
    "Just Add as Attachment" : function () {
        shouldIngest = false;
        $(this).dialog("close");
    }
 }

或者,由于您使用按钮来确定是否应该执行某些操作,因此只需跳过设置标志并执行操作即可:

buttons : {
    "Yes Ingest this Docx" : function () {
        callYouIngestionMethod();
        $(this).dialog("close");    
    },
    "Just Add as Attachment" : function () {
       // just close the dialog            
       $(this).dialog("close");
    }
 }

关于javascript - 确定 jquery 对话框的结果。将对话的结果分配给变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34358369/

相关文章:

javascript - jQuery : why is document. 就绪函数无法正常工作

jquery - 软键盘在显示时调整移动网络的大小

jquery - 更改 jQuery UI 中选项卡的 CSS

javascript - 尝试在 Google Apps 脚本中获取文件的文件夹路径

javascript - 如何动态更改 Angular JS 中类的内容?

javascript - 检查 $(this) 是否有在数组中指定类或 id 的 parent

jquery - 需要使用 jQuery animate() 调整背景图像大小的帮助..这可能吗?

jquery - 导航栏选项卡的自定义主题/颜色jquery-mobile

javascript - 等待 localhost 在 ASP.NET Core 中重新加载

javascript - 检测具有特定 ID 的元素何时插入 DOM 的最有效方法是什么