且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

带有布尔返回的 jQuery UI 对话框 - true 或 false

更新时间:2022-11-01 13:01:57

jQueryUI 对话框不能返回 truefalse,因为它们显示在顶部其他内容,但没有阻塞执行.

jQueryUI dialog boxes can't return a true or false as they're shown on top of other content but without blocking execution.

你能做的***的就是:

  1. 将框设为modal,使其隐藏其他内容

根据选择的选项提供要使用的回调.

supply callbacks to be used depending on which option is chosen.

对于额外的奖励积分,您可以创建一个 $.Deferred() 承诺对象并在您显示对话框时返回它.然后,您可以在按钮事件处理程序中 resolvereject 承诺.

For extra bonus points, you could create a $.Deferred() promise object and return that when you show the dialog. You can then resolve or reject that promise in the button event handlers.

这将使您在显示对话框和执行随后由它触发的操作之间清晰地分开:

This would give you clean separation between showing the dialog box, and performing the actions subsequently triggered by it:

function showDialog() {
   var def = $.Deferred();

   // create and/or show the dialog box here
   // but in "OK" do 'def.resolve()'
   // and in "cancel" do 'def.reject()'

   return def.promise();
}

showDialog().done(function() {
    // they pressed OK
}).fail(function() {
    // the pressed Cancel
});

// NB: execution will continue here immediately - you shouldn't do
//     anything else now - any subsequent operations need to be
//     started in the above callbacks.