在发布之前使用jQuery修改POST变量 [英] Modify POST vars before post, using jQuery

查看:92
本文介绍了在发布之前使用jQuery修改POST变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当用户提交表单时,我想修改(添加)POST请求的一些参数,在它从客户端发送到服务器之前。




  1. 用户点击'提交'

  2. 我的jQuery提交hander开始执行...

  3. 我创建了一些新的键/值对并将它们添加到POST有效载荷

目前看起来我唯一的选择是使用 $。post() $('form')。append('& lt; input type =hiddenname = ... value = ...');

感谢您的帮助。



编辑:我已经将提交处理程序附加到形成;我试图在用户点击提交按钮和发送到服务器的请求之间编辑post vars。

解决方案

使用表单上的 submit()函数创建回调。如果函数返回true,表单将被提交;

  $('#theForm')。submit(function(){
$(#field1).val(newValue1);
$(#field2).val(newValue2);
$(this).append(newFormField);
return true;
});

等。


I have a form, and a submit handler in jQuery.

When the user submits the form, I want to modify (add) some parameters to the POST request, before it is despatched from the client to the server.

i.e.

  1. User clicks 'submit'
  2. My jQuery submit hander begins execution...
  3. I create some new key/value pairs and add them to the POST payload

At the moment, it looks like my only options are to use $.post(), or $('form').append('<input type="hidden" name=... value=...');

Thanks for any help.

Edit: I've already attached a submit handler to the form; I'm trying to edit the post vars in between the user clicking the submit button, and the request being sent to the server.

解决方案

Use the submit() function on the form to create a callback. If the function returns true, the form will be submitted; if false, the form will not post.

$('#theForm').submit(function() {
    $("#field1").val(newValue1);
    $("#field2").val(newValue2);
    $(this).append(newFormField);
    return true;
});

etc.

这篇关于在发布之前使用jQuery修改POST变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆