Javascript 设置 jQuery UI 对话框按钮 ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10312835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:48:55  来源:igfitidea点击:

Set jQuery UI dialog button id?

javascriptjqueryjquery-ui

提问by kaze

Is it possible to set the ID for the buttons in a jQuery UI dialog, so that I can refer to them later through jQuery? For example, trigger events, disable etc?

是否可以在 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(); 

回答by mprabhat

$("#myDialog").dialog({
  buttons :  { 
     "MyButton" : {
         text: "OK",
         id: "okbtnid",
         click: function(){
             var bValid = true;
         }   
      } 
   }
});

回答by paulslater19

Or you can do it as an array:

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

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

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

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

回答by GillesC

Not through the way you want as the API doesn't provide those options however if you look at the markup generated by the dialog box you should be able to grab whichever elements you need and bind them as you want or add ids to them. Here is the markup as found of the documentation page (http://jqueryui.com/demos/dialog/)

不是通过您想要的方式,因为 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>

If it's buttons inside the content of the modal then you can do CSS queries in the modal element context and get access to them that way.

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