【问题标题】:Button is not working after I used jQuery Dialog使用 jQuery 对话框后按钮不起作用
【发布时间】:2013-12-26 03:03:10
【问题描述】:

我想使用 jquery 对话框制作注册表单,但是在我使用 jQuery 对话框并单击按钮打开表单后,#btnsubmit 按钮不起作用,在它起作用之前,有人可以帮助我解决我一直在尝试的问题现在找到了 2 天的答案,但找不到答案,我完全是新手:P

js

$(function () {
        $("#dialog").dialog({
            autoOpen: false,
            width: 630,
            height: 630,
            draggable: false,
            resizable: false,
            modal: true
        });

        $("#btnaddnew").click(function () {
            $("#dialog").dialog("open");
            return false;
        });
    });

html

<div id="dialog" title="Add/Edit User">
<asp:UpdatePanel ID="addpanel" runat="server">
<ContentTemplate>
<table>
    <tr>
        <td><div id="registerform">
              <div class="formrow">
                <table>
                <tr>
                    <td><label for="firstname">First Name</label></td>
                    <td><asp:TextBox ID="txfirstname" runat="server" class="basetxt" /></td>
                </tr>
                <tr>
                    <td><asp:RequiredFieldValidator ID="rfvfirstname" runat="server" ForeColor="Red" Display="Dynamic" ErrorMessage="Required" ControlToValidate="txfirstname" style="margin-left:20px" /></td>
                </tr>
                </table>
              </div>

              <div class="formrow">
              <table>
              <tr>
                <td><label for="lastname">Last Name</label></td>
                <td><asp:TextBox ID="txlastname" runat="server" class="basetxt" /></td>
              </tr>
              <tr>
                <td><asp:RequiredFieldValidator ID="rvflastname" runat="server" ForeColor="Red" ErrorMessage="Required" Display="Dynamic" ControlToValidate="txlastname" style="margin-left:20px" /></td>
              </tr>
              </table>
              </div>

              <div class="formrow">
              <table>
                <tr>
                    <td><label for="address">Address</label></td>
                    <td><asp:TextBox ID="txaddress" runat="server" class="basetxt" /></td>
                </tr>
                <tr>
                    <td><asp:RequiredFieldValidator ID="rfvaddress" runat="server" ForeColor="Red" Display="Dynamic" ErrorMessage="Required" ControlToValidate="txaddress" style="margin-left:20px" /></td>
                </tr>
              </table>
              </div>

              <div class="formrow">
              <table>
                <tr>
                    <td><label for="age">Age</label></td>
                    <td><asp:TextBox ID="txage" runat="server" class="basetxt" /></td>
                </tr>
                <tr>
                    <td><asp:RequiredFieldValidator ID="rvfage" runat="server" ForeColor="Red" Display="Dynamic" ErrorMessage="Required" ControlToValidate="txage" style="margin-left:20px" />
                    <asp:RegularExpressionValidator ID="revage" runat="server" ForeColor="Red" Display="Dynamic" ErrorMessage="Not Valid" ControlToValidate="txage" ValidationExpression="^[1-9]\d{0,2}$" /></td>
                </tr>
              </table>
              </div>

              <div class="formrow">
              <table>
                <tr>
                    <td><label for="mobile">Mobile #</label></td>
                    <td><asp:TextBox ID="txmobile" runat="server" class="basetxt" /></td>
                </tr>
                <tr>
                    <td><asp:RequiredFieldValidator ID="rvfmobile" runat="server" Display="Dynamic" ForeColor="Red" ErrorMessage="Required" ControlToValidate="txmobile" style="margin-left:20px" />
                    <asp:RegularExpressionValidator ID="revmobile" runat="server" ForeColor="Red" ErrorMessage="Not Valid" Display="Dynamic" ControlToValidate="txmobile" ValidationExpression="^\d{11}" style="margin-left:20px" /></td>
                </tr>
              </table>
              </div>

              <div id="btnc2">
              <asp:Button ID="btnsubmit" runat="server" class="submitbtn" Text="Submit" onclick="btnsubmit_Click" style="margin-left: 10px" />
              <asp:Button ID="btnreset" runat="server" class="submitbtn" Text="Reset" 
                      style="margin-left: 30px" onclick="btnreset_Click" CausesValidation="false" />
              </div></td>
    </tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>

按钮添加新

<asp:Button ID="btnaddnew" runat="server" class="submitbtn" Text="Add New" 
                            style="margin-left: 10px; margin-top: 10px" onclick="btnaddnew_Click" CausesValidation="false" />

【问题讨论】:

  • 在它工作之前......你做了什么改变......按钮是否是动态创建的
  • 之前,我没有放 jquery 对话框,然后我使用了对话框,在我单击按钮打开对话框并填写表单后没有任何反应,它应该让我成为我的用户数据库

标签: javascript jquery html forms dialog


【解决方案1】:

您应该使用jQuery .on 方法将点击处理程序分配给按钮元素。这是应该的样子:

$(function () {
    $("#dialog").dialog({
        autoOpen: false,
        width: 630,
        height: 630,
        draggable: false,
        resizable: false,
        modal: true
    });

    $("#dialog").on('click', '#btnaddnew', function () {
        $("#dialog").dialog("open");
        return false;
    });
});

【讨论】:

  • 为什么在通过 jQuery 方法分配处理程序时设置了onclick 属性?
  • 我的项目是制作一个对话框,里面有一个表单,它会询问您的个人信息,当您单击提交按钮时,它将添加到我的数据库中。对不起,我是编程新手,我还不擅长阅读代码
  • 我使用onclick 将表单提交到我的数据库并添加了一个新用户,但是在我使用 jQuery Dialog 之后它不再工作了,每次我点击提交按钮时都没有任何反应,没有用户添加到我的数据库中
猜你喜欢
  • 1970-01-01
  • 2011-07-10
  • 2014-08-02
  • 1970-01-01
  • 1970-01-01
  • 2013-07-06
  • 1970-01-01
  • 1970-01-01
  • 2015-02-12
相关资源
最近更新 更多