按钮点击事件在弹出窗口中不起作用

button click event not working in a pop up

本文关键字:窗口 不起作用 事件 按钮      更新时间:2024-02-20

在弹出窗口中按钮点击事件不会触发,但在弹出窗口外它工作正常,在Html中,注释部分是我在弹出窗口之外调用按钮事件的地方,它工作正常。但在弹出窗中,当我点击"保存"按钮时,什么都不会发生。这是我的代码

  <script>
    $(function () {
         $("#dialog-1").dialog({
             autoOpen: false,
         });
         $("#opener").click(function () {
             $("#dialog-1").dialog("open");
         });
     });
  </script>

Html

         <%--<asp:Button Text="save" class="btn btn-danger" runat="server"  CausesValidation="false" OnClick="SaveDB" />
           <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>--%>
<button type="button" class="btn btn-warning" id="opener" > Create Database       </button>         
               <div id="dialog-1" title="Add new Database">
    Database name: <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
    <asp:Button Text="Save" class="btn btn-danger" runat="server"  CausesValidation="false" OnClick="SaveDB" style="padding-left:2%;padding-right:2%;" /> 
  </div>

aspx.cs文件代码

   protected void SaveDB(object sender, EventArgs e)
  { 
       TextBox1.Text="It works";
  }

尝试将事件处理程序附加到body,如下所示:

$("body").on("click", "#opener", function(){
    $("#dialog-1").dialog("open");
});

问题可能是在设置处理程序时,DOM中不存在#opener元素,但附加到body可以解决这个问题。