简体   繁体   English

在提交之前修改表单数据

[英]Modifying form data before submission

I'm using jQuery in conjunction with the form plugin and I'd like to intercept the form data before submission and make changes. 我正在将jQuery与表单插件结合使用,我想在提交之前拦截表单数据并进行更改。

The form plugin has a property called beforeSubmit that should do this, but I seem to be having trouble getting the function I specify to run. 表单插件有一个名为beforeSubmit的属性应该这样做,但我似乎无法获取我指定运行的函数。

Here's the markup for the form (some style details omitted): 这是表单的标记(省略了一些样式细节):

<form id="form1">
    <fieldset id="login">
        <legend>Please Log In</legend>
        <label for="txtLogin">Login</label>
        <input id="txtLogin" type="text" />
        <label for="txtPassword">Password</label>
        <input id="txtPassword" type="password" />
        <button type="submit" id="btnLogin">Log In</button>
    </fieldset>
</form>

And here's the javascript that I have so far: 这是我到目前为止的javascript:

$(document).ready(function() {
    var options = {
        method: 'post',
        url: 'Login.aspx',
        beforeSubmit: function(formData, form, options) {
            $.each(formData, function() { log.info(this.value); });
            return true;
        }
    };
    $('form#form1').ajaxForm(options);
});

(log.info() is from the Blackbird debugger library I'm using) (log.info()来自我正在使用的Blackbird调试器库)

When I click the submit button, rather than the POST verb I specified it uses a GET instead, and nothing is logged from my beforeSubmit function. 当我单击提交按钮时,而不是我指定的POST动词,而是使用GET,而且我的beforeSubmit函数没有记录任何内容。 It seems that the ajaxForm plugin is not being applied to the form at all, but I don't see why. 似乎ajaxForm插件根本没有应用于表单,但我不明白为什么。 Can anybody help with this? 任何人都可以帮忙吗?

I ran the following code through firebug and it appears to work as advertised, but the formData variable in the beforeSubmit callback is empty because you didn't set the name attribute on the text boxes. 我通过firebug运行以下代码,它看起来像宣传的那样工作,但是beforeSubmit回调中的formData变量是空的,因为你没有在文本框上设置name属性。

<script type="text/javascript">
  $(document).ready(function() {
    var options = {
      beforeSubmit: showData
    };
    $('form#form1').ajaxForm(options);
  });
  function showData(formData, form, options) {
    //var formData = [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ];
    $.each(formData, function(i, obj) { log.info(obj.name + " | " + obj.value); });
    return true;
  }
</script>

<form id="form1" action="Login.aspx" method="post">
<fieldset id="login">
    <legend>Please Log In</legend>
    <label for="txtLogin">Login</label>
    <input id="txtLogin" type="text" name="User" />
    <label for="txtPassword">Password</label>
    <input id="txtPassword" type="password" name="Pass" />
    <button type="submit" id="btnLogin">Log In</button>
</fieldset>
</form>

For starters, according to this API , your options object should use type and not method , or just specify the method attribute on the form in the HTML. 对于初学者,根据此API ,您的选项对象应使用type而不是method ,或者只在HTML中的表单上指定method属性。

(now I'll throw in a couple minor stylistic notes; you can stop reading here if you want): (现在我会抛出几个小风格的笔记;如果你愿意,你可以在这里停止阅读):

  1. You can replace $(document).ready(function... with just $(function... . 您可以替换$(document).ready(function...只需$(function...
  2. $.each(formData, function... looks more natural as $(formData).each(function... $.each(formData, function...看起来更自然为$(formData).each(function...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM