jQuery $(this).serialize() -- 如何添加一个值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6539502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$(this).serialize() -- How to add a value?
提问by TheExit
currently I have the following:
目前我有以下几点:
$.ajax({
type: 'POST',
url: this.action,
data: $(this).serialize(),
});
This works fine, however I would like to add a value to data, so I tried
这工作正常,但是我想为数据添加一个值,所以我尝试了
$.ajax({
type: 'POST',
url: this.action,
data: $(this).serialize() + '&=NonFormValue' + NonFormValue,
});
But that didn't post correctly. Any ideas on how you can add an item to the serialize string? This is a global page variable that isn't form specific.
但这没有正确发布。关于如何将项目添加到序列化字符串的任何想法?这是一个非表单特定的全局页面变量。
采纳答案by matt b
Instead of
代替
data: $(this).serialize() + '&=NonFormValue' + NonFormValue,
you probably want
你可能想要
data: $(this).serialize() + '&NonFormValue=' + NonFormValue,
You should be careful to URL-encode the value of NonFormValue
if it might contain any special characters.
NonFormValue
如果它可能包含任何特殊字符,您应该小心对值进行 URL 编码。
回答by Leigh Brenecki
While matt b's answer will work, you can also use .serializeArray()
to get an array from the form data, modify it, and use jQuery.param()
to convert it to a url-encoded form. This way, jQuery handles the serialisation of your extra data for you.
虽然 matt b 的答案会起作用,但您也可以使用.serializeArray()
它从表单数据中获取一个数组,对其进行修改,然后jQuery.param()
将其转换为 url 编码的表单。这样,jQuery 会为您处理额外数据的序列化。
var data = $(this).serializeArray(); // convert form to array
data.push({name: "NonFormValue", value: NonFormValue});
$.ajax({
type: 'POST',
url: this.action,
data: $.param(data),
});
回答by Nikhil Bhandari
firstly shouldn't
首先不应该
data: $(this).serialize() + '&=NonFormValue' + NonFormValue,
be
是
data: $(this).serialize() + '&NonFormValue=' + NonFormValue,
and secondly you can use
其次你可以使用
url: this.action + '?NonFormValue=' + NonFormValue,
or if the action already contains any parameters
或者如果动作已经包含任何参数
url: this.action + '&NonFormValue=' + NonFormValue,
回答by Mrchief
Add the item first and then serialize:
先添加项目,然后序列化:
$.ajax({
type: 'POST',
url: this.action,
data: $.extend($(this), {'NonFormValue': NonFormValue}).serialize()
});
回答by Jonathan
Don't forget you can always do:
不要忘记你总是可以这样做:
<input type="hidden" name="NonFormName" value="NonFormValue" />
in your actual form, which may be better for your code depending on the case.
以您的实际形式,根据情况,这可能对您的代码更好。
回答by Tapeshwar
We can do like:
我们可以这样做:
data = $form.serialize() + "&foo=bar";
For example:
例如:
var userData = localStorage.getItem("userFormSerializeData");
var userId = localStorage.getItem("userId");
$.ajax({
type: "POST",
url: postUrl,
data: $(form).serialize() + "&" + userData + "&userId=" + userId,
dataType: 'json',
success: function (response) {
//do something
}
});
回答by eugene
this better:
这个更好:
data: [$(this).serialize(),$.param({NonFormValue: NonFormValue})].join('&')
回答by Jithin Vijayan
You can write an extra function to process form data and you should add your nonform data as the data valu in the form.seethe example :
您可以编写一个额外的函数来处理表单数据,并且您应该将非表单数据添加为表单中的数据值。请参见示例:
<form method="POST" id="add-form">
<div class="form-group required ">
<label for="key">Enter key</label>
<input type="text" name="key" id="key" data-nonformdata="hai"/>
</div>
<div class="form-group required ">
<label for="name">Ente Name</label>
<input type="text" name="name" id="name" data-nonformdata="hello"/>
</div>
<input type="submit" id="add-formdata-btn" value="submit">
</form>
Then add this jquery for form processing
然后添加这个jquery进行表单处理
<script>
$(document).onready(function(){
$('#add-form').submit(function(event){
event.preventDefault();
var formData = $("form").serializeArray();
formData = processFormData(formData);
// write further code here---->
});
});
processFormData(formData)
{
var data = formData;
data.forEach(function(object){
$('#add-form input').each(function(){
if(this.name == object.name){
var nonformData = $(this).data("nonformdata");
formData.push({name:this.name,value:nonformData});
}
});
});
return formData;
}