22

是否可以在 jQuery UI 对话框中设置按钮的 ID,以便稍后通过 jQuery 引用它们?例如,触发事件、禁用等?

... in the dialog setup ...
buttons: {               
    "Sök": function () {
        var bValid = true;
    },
    "OK": function () {
        if (OK()) {
            getStuffNoteringar($("#txtStuffId").val());
            $(this).dialog("close");
        }
    }

.... later on in some javascript code....

$('#OK').click(); 
4

3 回答 3

48
$("#myDialog").dialog({
  buttons :  { 
     "MyButton" : {
         text: "OK",
         id: "okbtnid",
         click: function(){
             var bValid = true;
         }   
      } 
   }
});
于 2012-04-25T09:33:24.623 回答
2

或者您可以将其作为数组进行:

$("#myDialog").dialog({
   buttons :  [{ 
     text: "OK",
     id: "ok",
     click: function(){
         alert("clicked");
     }   
   }]
});

http://docs.jquery.com/UI/Dialog

于 2012-04-25T09:36:16.793 回答
0

不是通过您想要的方式,因为 API 不提供这些选项,但是如果您查看对话框生成的标记,您应该能够获取您需要的任何元素并根据需要绑定它们或向它们添加 id。这是文档页面 (http://jqueryui.com/demos/dialog/) 中的标记

<div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-draggable ui-resizable">
   <div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix">
      <span id="ui-dialog-title-dialog" class="ui-dialog-title">Dialog title</span>
      <a class="ui-dialog-titlebar-close ui-corner-all" href="#"><span class="ui-icon ui-icon-closethick">close</span></a>
   </div>
   <div style="height: 200px; min-height: 109px; width: auto;" class="ui-dialog-content ui-widget-content" id="dialog">
      <p>Dialog content goes here.</p>
   </div>
</div>

如果它是模态内容中的按钮,那么您可以在模态元素上下文中进行 CSS 查询并以这种方式访问​​它们。

于 2012-04-25T09:33:08.723 回答