2012-04-25 132 views
14

是否可以在jQuery UI對話框中設置按鈕的ID,以便以後可以通過jQuery來引用它們?例如,觸發事件,禁用等?設置jQuery UI對話框按鈕ID?

... 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(); 

回答

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

尼斯不知道會工作不證明,但它確實。 – GillesC 2012-04-25 09:37:02

+2

[文檔](http://jqueryui.com/demos/dialog/#option-buttons)不說有關身份證,但它有文本,並單擊:) – mprabhat 2012-04-25 09:38:03

+0

是的,但這兩個都不是屬性,所以我想設置屬性不起作用。很高興能夠了解這種無證的功能。從文檔中遺漏的那些永遠是最好的知道:) – GillesC 2012-04-25 09:42:14

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查詢上下文並以這種方式訪問​​它們。