2014-10-30 92 views
1

我有现在设置为特定高度的jQuery UI的对话框:自动调整大小jQuery的对话框

$(".event").click(function() { 
     var id=$(this).attr("data-id"); 

     $("<div></div>") 

      .addClass("modal-dialog") 
      .appendTo("body") 

      .dialog({ 
       close: function() { $(this).remove(); }, 
       modal: true, 
       height: 600, 
       width: 700 
      }) 
      .load("/Home/users?xid=" + id); 

    }); 

我想为我的对话框取决于它的内容调整它的高度。 我试图更改为height: auto,但它根本无法打开。

我也发现此线程: Automatically resize jQuery UI dialog to the width of the content loaded by ajax 但我不明白如何将它应用到我自己的代码。

回答

0

什么,我会做的是加载到一个子元素,然后在对话框中设置高度为孩子的身高在load回调:

$(".event").click(function() { 
    var id=$(this).attr("data-id"); 

    var dialogElement = $("<div></div>") 
     .addClass("modal-dialog") 
     .appendTo("body") 
     .dialog({ 
      close: function() { $(this).remove(); }, 
      modal: true, 
      height: 600, 
      width: 700 
     }); 
    $("<div></div>").appendTo(dialogElement).load("/Home/users?xid=" + id, function(){ 
     dialogElement.dialog("height", $(this).height()); 
    }); 

}); 

您可能要真正使它比孩子稍大,这样所有的用户界面都有空间。

+0

这是非常好的,它的工作。只是一件事。使用这段代码,对话框在页面上打开的很少。我有办法决定我想放置在哪里吗? 没有足够的代表upvote,但非常感谢! – Wranglerino 2014-10-30 07:22:52

+0

http://api.jqueryui.com/dialog/#option-position您可以在设置高度时将位置设置回默认值。 – Scimonster 2014-10-30 07:26:02

相关问题