代码之家  ›  专栏  ›  技术社区  ›  TomSelleck

提交表单前执行方法

  •  1
  • TomSelleck  · 技术社区  · 7 年前

    我有以下代码来提交一个隐藏值的表单。

    问题是-我无法设置 MyNewValue 在表单提交到远程地址之前 https://some-remote-site/Form.aspx .

    MyFile.aspx文件

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body style="height: 370px">    
        <form id="myForm" method="post" runat="server" action="https://some-remote-site/Form.aspx">
            <fieldset>
                <asp:HiddenField id="MyNewValue" Value="X" runat="server" />
                <p>
                    <label for="Username">Username:</label>
                    <asp:TextBox id="Username" Text="Username" runat="server" />
                </p>
                <asp:Button ID="SubmitForm" runat="server" onclick="SubmitForm_Click" Text="Button" />
            </fieldset>
        </form>
    </body>
    

    MyFile.aspx.cs文件

    // Not executed because action="https://some-remote-site/Form.aspx" is set
    protected void SubmitForm_Click(object sender, EventArgs e)
    {
        MyNewValue.Value = DateTime.Now.ToString();
    }
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   Majdi Saibi    7 年前

    您可以使用jQuery在客户端完成它。因此,当用户单击提交时,在提交表单之前,可以更新隐藏的输入,然后返回true以继续提交表单。

    这是可以做到的:

    $(document).ready(function(){
        $('#myForm').on('submit', function(){
            $('#MyNewValue').val('YOUR_VALUE');
            return true;
        })
    });