Skip to main content
 首页 » 编程设计

javascript之在 jquery 模式弹出窗口中未触发按钮单击事件

2024年02月06日35lhb25

我实现了一个 jquery 模态弹出窗口

Jquery 代码:-

<script type="text/javascript"> 
    $(function(){ 
     $('#<%=a2.ClientID %>').click(function(){ 
     $('#modelPopup').dialog({ 
     title: "Add New Server", 
     width:650, 
     height:450, 
     modal:true, 
     buttons:{ 
      Close:function(){ 
      $(this).dialog('close'); 
      } 
     } 
     }); 
    });     
    }) 
    </script> 

HTML:-

<asp:Button ID="btnAddNewServic" Text="Add Service" runat="server"  
 CssClass="btnSmall_bg_green" Height="26px" Width="98px" OnClick="btnAddNewServic_Click" /> 

代码隐藏:-

 protected void btnAddNewServic_Click(object sender, EventArgs e) 
        { 
            int rowCount = 0; 
            rowCount = Convert.ToInt32(Session["clicks"]); 
            rowCount++; 
 
            Session["clicks"] = rowCount; 
            for (int i = 0; i < rowCount; i++) 
            { 
                TextBox txtServerU = new TextBox(); 
                Label lblU = new Label(); 
 
                txtServerU.ID = "txtServerU" + i.ToString(); 
                lblU.ID = "lblU" + i.ToString(); 
 
                lblU.Text = "Service" + (i + 1).ToString() + ":"; 
 
                panelnewserver.Controls.Add(lblU); 
                panelnewserver.Controls.Add(txtServerU); 
 
            } 
        } 

我找不到这段代码的问题。谁能帮我触发这个按钮点击事件。提前致谢。

更新:-

JQuery 代码:-

$(function(){ 
     $('#<%=a2.ClientID %>').click(function(){ 
     var $dialog= $('#modelPopup').dialog({ 
     autoopen:false,      
     title: "Add New Server", 
     width:650, 
     height:450, 
     modal:true,         
     buttons:{ 
      Close:function(){ 
      $(this).dialog('close'); 
      } 
     }      
     }); 
      $dialog.parent().appendTo($("form:first")); 
      return false; 
    });     
    }) 
    </script> 

现在触发了按钮单击事件,但在完成我的过程后,模态弹出窗口将自动关闭。谁能帮我解决这个问题。

请您参考如下方法:

根据我的内存,我相信 jQuery UI 对话框在显示它们时实际上会将对话框元素从 DOM 中移出它的位置。因为这会导致您的 btnAddNewServic 被移出 form,您的回传将不会被正确触发。

所以像这样的东西可能对你的 JavaScript 有用:

$(function(){ 
    var $dialog = $('#modelPopup'); 
 
    $dialog.dialog({ 
        autoOpen: false, 
        title: "Add New Server", 
        width:650, 
        height:450, 
        modal:true, 
        buttons:{ 
            Close:function(){ 
                $(this).dialog('close'); 
            } 
        } 
    }); 
 
    $dialog.parent().appendTo($("form:first")); 
 
    $('#<%=a2.ClientID %>').click(function(){ 
        $dialog.dialog('open'); 
    });     
}) 

这样做的目的是在对话框创建后将其添加回表单,因此您的回传现在应该运行。