在 jQuery serialize() 或 serializeArray() 中向 Ajax POST 添加/push() 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4449695/
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
Adding/push() Values to Ajax POST in jQuery serialize() or serializeArray()
提问by hoerf
jQuery
jQuery
$('#speichern').live('click' , function () {
// [a] var data_save = $('#form_rechn').serializeArray();
var data_save_ser = $('#form_rechn').serialize(); //[b]
// [a] data_save[data_save.length] = {"name":"action","value":"save" },{"name":"total","value": Number($('#grandTotal').text().replace(/EUR/g, ""))};
var addintional = 'action=save&mysql=update' + '&' + 'total=' + Number($('#grandTotal').text().replace(/EUR/g, ""));//[b]
var data_save = data_save_ser + '&' + addintional;//[b]
$.ajax({
type : "POST",
cache : false,
url : 'invoice_new_action.php',
data : data_save,
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
},
success : function(data) {
$.fancybox(data);
}
});
});
The [b]-part works very well; however, why does not work the [a]-part? This is not pushed:
,{"name":"total","value": [..]
[b] 部分效果很好;但是,为什么 [a] 部分不起作用?这不是推:
,{"name":"total","value": [..]
php Ouput via print_r ($_POST)
php 输出通过 print_r ($_POST)
[b]-version
[b]-版本
Array ( [pnr_item_1] => 1 [pkt_item_1] => HostingXXL [desc_item_1] => 20GB, 1x.de [qty_item_1] => 4 [price_item_1] => 15.5 .... [action] => save [mysql] => update [total] => 62 )
Array ( [pnr_item_1] => 1 [pkt_item_1] => HostingXXL [desc_item_1] => 20GB, 1x.de [qty_item_1] => 4 [price_item_1] => 15.5 .... [action] => save [mysql] => update [total] => 62 )
[a]-version
[厌恶
Array ( [pnr_item_1] => 1 [pkt_item_1] => HostingXXL [desc_item_1] => 20GB, 1x.de [qty_item_1] => 4 [price_item_1] => 15.5 .... [action] => save )
Array ( [pnr_item_1] => 1 [pkt_item_1] => HostingXXL [desc_item_1] => 20GB, 1x.de [qty_item_1] => 4 [price_item_1] => 15.5 .... [action] => save )
Hopefully my problem/question is clear. What is the best method? There are better methods to so id?
希望我的问题/问题很清楚。最好的方法是什么?有没有更好的方法来so id?
回答by Nick Craver
It should look like this:
它应该是这样的:
$('#speichern').live('click' , function () {
var data_save = $('#form_rechn').serializeArray();
data_save.push({ name: "action", value: "save" });
data_save.push({ name: "mysql", value: "update" });
data_save.push({ name: "total", value: Number($('#grandTotal').text().replace(/EUR/g, "")) });
$.ajax({
type : "POST",
cache : false,
url : 'invoice_new_action.php',
data : data_save,
error : function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
},
success : function(data) {
$.fancybox(data);
}
});
});
What you want to push onto the array are objects in the form of {name: "name", value: "value"}
, then they'll be serialized/encoded correctly. Option [a]
(the corrected form of it) is generally muchbetter than option [b]
, since [b]
isn't property encoded, and will fail the moment any invalid character slips in there to mess up your variables. In this case, because you're in control of the appended content, you're safe...but it's best to go the route that always works: never creating your data
argument as a string directly.
您想要推送到数组的是 形式的对象{name: "name", value: "value"}
,然后它们将被正确序列化/编码。选项[a]
(它的经校正的形式)一般多比选择好[b]
,因为[b]
不是财产进行编码,将失败的时刻,没有任何无效字符单搞砸了你的变量。在这种情况下,因为您可以控制附加的内容,所以您很安全……但最好走始终有效的路线:永远不要data
直接将参数创建为字符串。
As for the why [a]
doesn't work:
至于为什么[a]
不起作用:
data_save[data_save.length] = {"name":"action","value":"save" },{"name":"total","value": Number($('#grandTotal').text().replace(/EUR/g, ""))};
This isn't valid, you can't assign 2 things at once, you either need to do it like this:
这是无效的,您不能一次分配两件事,您要么需要这样做:
data_save[data_save.length] = {"name":"action","value":"save" };
data_save[data_save.length] = {"name":"total","value": Number($('#grandTotal').text().replace(/EUR/g, ""))};
or this (my preferred method, as used above):
或者这个(我的首选方法,如上所述):
data_save.push({"name":"action","value":"save" });
data_save.push({"name":"total","value": Number($('#grandTotal').text().replace(/EUR/g, ""))});
....or, use $.merge()
(a bit more wasteful, but cleaner looking), like this:
....或者,使用$.merge()
(有点浪费,但看起来更干净),像这样:
$.merge(data_save, [{"name":"action","value":"save" }, {"name":"total","value": Number($('#grandTotal').text().replace(/EUR/g, ""))}]);
回答by Shaishav Shah
You can combine both the forms and serializeArray
您可以结合两种形式和 serializeArray
$('#frm1, #frm2').serializeArray()