的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);
}
});
});
[b] -part效果很好;但是,为什么不能[a] -part工作?这不是推动:
,{"name":"total","value": [..]
php输出
[b] -version [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 [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 )
答案 0 :(得分:28)
它应该是这样的:
$('#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);
}
});
});
你想要推送到数组的是{name: "name", value: "value"}
形式的对象,然后它们将被正确地序列化/编码。选项[a]
(其更正形式)通常比选项[b]
更强更多,因为[b]
不是属性编码的,并且会在任何时候失败无效的字符在那里滑倒以搞乱你的变量。在这种情况下,因为你控制了附加的内容,所以你是安全的......但是最好是总是有效的路径:永远不要直接将data
参数创建为字符串。
至于[a]
不起作用的原因:
data_save[data_save.length] = {"name":"action","value":"save" },{"name":"total","value": Number($('#grandTotal').text().replace(/EUR/g, ""))};
这是无效的,你不能一次分配2件事,你需要这样做:
data_save[data_save.length] = {"name":"action","value":"save" };
data_save[data_save.length] = {"name":"total","value": Number($('#grandTotal').text().replace(/EUR/g, ""))};
或者这(我的首选方法,如上所用):
data_save.push({"name":"action","value":"save" });
data_save.push({"name":"total","value": Number($('#grandTotal').text().replace(/EUR/g, ""))});
....或者,使用$.merge()
(有点浪费,但看起来更干净),像这样:
$.merge(data_save, [{"name":"action","value":"save" }, {"name":"total","value": Number($('#grandTotal').text().replace(/EUR/g, ""))}]);
答案 1 :(得分:2)
您可以同时组合表单和serializeArray
$('#frm1, #frm2').serializeArray()