[英]serialized parameters with jquery for ajax
I need help to prepare parameters for $.ajax submit. 我需要帮助来准备$ .ajax提交的参数。
I have multiple pairs of html inputs (i pairs): 我有多对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">
if I use this jquery code: 如果我使用以下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
});
};
I send parameters like this: 我发送这样的参数:
product_id=31&product_id=30&quantity=2&quantity=2
product_id = 31&product_id = 30&quantity = 2&quantity = 2
How pass parameters like this: 如何传递这样的参数:
product_id=31&quantity=2&product_id=30&quantity=2
product_id = 31&quantity = 2&product_id = 30&quantity = 2
Thanks 谢谢
if you want parse them in same order they are in html do 如果您想以相同的顺序解析它们,它们在html中
data = '';
$('input[type="hidden"]').each(function(){
data += '&' + $(this).attr('name') + '=' + $(this).attr('value');'
});
Do following change 进行以下更改
In html 在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>
in js 在js中
function send(){
$.ajax({
type: "POST",
url: 'index.php?route=checkout/cart/add',
data: jQuery('#frm').serialize()
});
};
you will get value in global $_POST 您将获得全球$ _POST的价值
try this: 尝试这个:
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.