2013-07-02 73 views
0

我在aspx页面上有以下标记,其中一个元素应该打开jQuery对话框。这里是标记,JQuery打开对话框

<asp:Repeater ID="Repeater1" runat="server" > 
    <HeaderTemplate> 
    <table cellpadding="5" cellspacing="0" > 
    <tr align="center" > 
     <th align="center"><span><a href="#" style="color:Blue;background-color:#f2f2f2;" >Open Dialog</a></span></th> 
    </tr> 
    ....... 
</asp:Repeater> 

我想下面的JQuery函数打开对话框,但没有工作,

$("th span a").click(function (e) { 
      e.preventDefault(); 
      var targetUrl = $(this).attr("href"); 

      $("#dialog").dialog({ 
       buttons: { 
        "Close": function() { 
         $(this).dialog("close"); 
        } 
       } 
      }); 

      $("#dialog").dialog("open"); 
     }); 

任何想法,缺什么?

+0

首先,通过不设置'autoOpen:false'选项,您试图打开它两次。正如所写的,你不需要'$(“#dialog”)。对话框(“打开”);',但是,离开它并将对话框初始化移动到点击事件之外可能是明智的做法 – 2013-07-02 19:28:40

+0

试试在'dialog'之前添加'ContentPlaceHolder_'。 – PiLHA

回答

2

首先,您是否有ID为dialog的元素?其次,你在click事件上创建一个已经打开的对话框,然后尝试再次打开它。我建议像重写:如果对话框doesnt已经存在

$(function() { 
    $(document).on('click', 'th span a', function(e) { 
     e.preventDefault(); 
     var targetUrl = $(this).attr("href"); 
     $("#dialog").dialog("open"); 
    }); 

    $('#dialog').dialog({ 
     autoOpen: false, 
     buttons: { 
      'Close': function(e, ui) { 
       $(this).dialog('close'); 
      } 
     } 
    }) 
}) 

,那么你可以为改写:

<script type="text/javascript"> 
    var dlg; 
    $(function() { 
     $(document).on('click', 'th span a', function(e) { 
      e.preventDefault(); 
      var targetUrl = $(this).attr("href"); 
      dlg.dialog("open"); 
     }); 

     dlg = $('<div />', { id: 'dialog' }).dialog({ 
      autoOpen: false, 
      buttons: { 
       'Close': function(e, ui) { 
        $(this).dialog('close'); 
       } 
      } 
     }) 
    }) 
</script> 

当然,这第二种方式意味着创建和附加HTML的对话框DIV,否则它将是空的......永远!

+0

谢谢@ bilbob533。你的代码工作正常..是的,我有对话框ID的元素。 – Rishi