如何将变量添加到我想通过 ajax 向其发送信息的数据字段的名称。
这是我的代码示例。
var qty = $('#qty_'+value).val();
$.ajax({
url: 'ajax/save_order-2.php?id='+value,
type: 'POST',
data: {
qty_'+value': qty
},
dataType: 'HTML',
success: function(order_info_response){
console.log(order_info_response);
}
});
如何将变量“value”附加到数据名称“qty”,就像我对 var qty 所做的那样。
执行此操作而不会出现语法错误的正确格式是什么?
最佳答案
您需要创建数组并传递它
var qty = $('#qty_'+value).val();
var data = {};
data["qty_"+value] = qty;
$.ajax({
url: 'ajax/save_order-2.php?id='+value,
type: 'POST',
data,
dataType: 'HTML',
success: function(order_info_response){
console.log(order_info_response);
}
});
关于javascript - jQuery ajax,附有var的数据字段的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32301484/