带有传递动态数据的jQuery表单插件

时间:2012-04-21 19:50:17

标签: javascript jquery jquery-plugins

我正在使用jquery表单插件。这是我的样本表格

<form id='formId' action='<?php echo $_SERVER['PHP_SELF']l?>' method='POST'>
<input type='text' name='username'/>
</form>

这是我的Javascript

$('#formId').ajaxForm({
    success:function(data){
       console.log(data)
    }
})

在这种情况下,我想动态传递另一个数据值,而不是在HTML中实现它。例如,我想传递密码字段。有没有任何方法可以实现这个选项?

2 个答案:

答案 0 :(得分:2)

旧问题,但这是一个有效的代码片段 该方法将以与&#39; Ohgodwhy&#39;相同的方式实施。回答

beforeSubmit: function(formData, formObject, formOptions){        
    formData.push(
        {name: 'password',value: 'password/variable'},
        {name: 'username',value: 'username/variable'}
    );
}

您需要将param名称和param值作为这样的集合传递。

答案 1 :(得分:-1)

This Page开始,beforeSubmit函数允许您挂钩提交的表单数据并注入/修改数据。

它允许3个参数,表单呈现为数组(这意味着我们可以推送到它),表单呈现为jquery Object(用于序列化),以及与ajaxForm / ajaxSubmit一起使用的options对象。工作示例 - &gt;

$('#formId').ajaxForm({
  beforeSubmit: function(formData, formObject, formOptions){
    //formData is the form array. We can push the password into the array before we submit the form.
    formData.push({
      password: //this is where you put the password you want
    });
  },
  success:function(data){
   console.log(data)
  }
});