[英]serialized parameters with jquery for ajax
我需要帮助来准备$ .ajax提交的参数。
我有多对html输入(i对):
> <input type="hidden" value="31" name="product_id"> <input
> type="hidden" value="3" name="quantity">
>
> <input type="hidden" value="34" name="product_id"> <input
> type="hidden" value="1" name="quantity">
如果我使用以下jQuery代码:
function send(){
var parameter1 =$("input[name$='product_id']").serialize();
var parameter2 = $("input[name$='quantity']").serialize();
$.ajax({
type: "POST",
url: 'index.php?route=checkout/cart/add',
data: parameter1+'&'+parameter2
});
};
我发送这样的参数:
product_id = 31&product_id = 30&quantity = 2&quantity = 2
如何传递这样的参数:
product_id = 31&quantity = 2&product_id = 30&quantity = 2
谢谢
如果您想以相同的顺序解析它们,它们在html中
data = '';
$('input[type="hidden"]').each(function(){
data += '&' + $(this).attr('name') + '=' + $(this).attr('value');'
});
进行以下更改
在html中
<form id="frm">
<input type="hidden" value="31" name="product_id[]">
<input type="hidden" value="3" name="quantity[]">
<input type="hidden" value="34" name="product_id[]">
<input type="hidden" value="1" name="quantity[]">
</form>
在js中
function send(){
$.ajax({
type: "POST",
url: 'index.php?route=checkout/cart/add',
data: jQuery('#frm').serialize()
});
};
您将获得全球$ _POST的价值
尝试这个:
function send(){
var data = {
parameter1:$("input[name$='product_id']").val(),
parameter2:$("input[name$='quantity']").val()
};
$.ajax({
type: "POST",
url: 'index.php?route=checkout/cart/add',
data: JSON.stringify(data),
success : function(msg){
// your code here
},
error : function(msg){
// your code here
},
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.