2010-10-27 94 views
15

我无法将按钮添加到此jQuery UI对话框。如果可能请给我一个例子。 谢谢。将按钮添加到jQuery UI对话框

<script type="text/javascript"> 
    $(document).ready(function() { 
     //setup new person dialog 
     $('#dialog2').dialog({ 
      autoResize: true, 
      show: "clip", 
      hide: "clip", 
      height: 'auto', 
      width: '1000', 
      autoOpen: false, 
      modal: true, 
      position: 'top', 
      draggable: false, 
      title: "انتخاب درخواست", 
      open: function (type, data) { 
       $(this).parent().appendTo("form"); 
      } 
     }); 

     $('#viewfaktor').dialog({ 
      autoResize: true, 
      show: "clip", 
      hide: "clip", 
      height: 'auto', 
      width: '1000', 
      autoOpen: false, 
      modal: true, 
      position: 'top', 
      draggable: true, 
      title: "مشاهده صورت ریز", 
      open: function (type, data) { 
       $(this).parent().appendTo("form"); 
      } 
     }); 


     $('#msgBox').dialog({ 


      autoResize: true, 
      show: "clip", 
      hide: "clip", 
      height: 'auto', 
      width: 'auto', 
      autoOpen: false, 
      modal: true, 
      position: 'center', 
      draggable: false, 



      open: function (type, data) { 
       $(this).parent().appendTo("form"); 
      } 


     }); 



    }); 

    function showDialog(id) { 
     $('#' + id).dialog("open"); 
    } 

    function closeDialog(id) { 
     $('#' + id).dialog("destroy"); 
    } 



</script> 

回答

23
$('#msgBox').dialog({ 
    autoResize: true, 
    show: "clip", 
    hide: "clip", 
    height: 'auto', 
    width: 'auto', 
    autoOpen: false, 
    modal: true, 
    position: 'center', 
    draggable: false, 

    open: function (type, data) { 
     $(this).parent().appendTo("form"); 
    }, 

    buttons: { "OK": function() { $(this).dialog("close"); } } 
}); 
+0

,你再看看我的问题。我有错误 – Shahin 2010-10-27 09:35:43

+0

我没有添加你的代码,但我有语法错误! – Shahin 2010-10-27 09:37:40

+2

啊。我省略了一个逗号。我更新了这个例子。 – 2010-10-27 09:42:21

6

Here is an example

添加到您的函数:

buttons: { 
       OK: function() { //submit 
        $(this).dialog("close"); 
       }, 
       Cancel: function() { //cancel 
        $(this).dialog("close"); 
       } 
      } 

所以,你得到

$('#msgBox').dialog({ 


       autoResize: true, 
       show: "clip", 
       hide: "clip", 
       height: 'auto', 
       width: 'auto', 
       autoOpen: false, 
       modal: true, 
       position: 'center', 
       draggable: false, 
       buttons: { 
       OK: function() { //ok 
        $(this).dialog("close"); 
       }, 
       Cancel: function() { //cancel 
        $(this).dialog("close"); 
       } 
      } 
       open: function (type, data) { 
        $(this).parent().appendTo("form"); 
       } 


      }); 
24

有时要动态地添加按钮后的对话框创建也是。见我answer的问题Add a button to a dialog box dynamically

var mydialog = ... result of jqueryui .dialog() 
var buttons = mydialog.dialog("option", "buttons"); // getter 
$.extend(buttons, { foo: function() { alert('foo'); } }); 
mydialog.dialog("option", "buttons", buttons); // setter 
+0

你将如何添加一个具有特定ID的按钮? – 2012-01-13 12:22:19

+0

为什么你需要按钮上的ID? – JJS 2012-01-15 16:34:30

+0

还是你的意思是添加一个由ID标识的现有按钮? – JJS 2012-01-15 16:41:19

10

如果你想将按钮添加到已经打开,你可以这样做一个对话框:

var buttonSet = $('#dialog').parent().find('.ui-dialog-buttonset'); 
var newButton = $('<button>My New Button</button>'); 
newButton.button().click(function() { 
    alert('My new button clicked'); 
}); 
buttonSet.append(newButton);