如何将变量用于对象的属性名称?
var foo = 'abc';
var value = '123';
var name = 'bar';
$.ajax({
url: foo.php,
data: { code : foo , name : value }, // passed name (should be bar)
type: 'POST',
success: function(data){}
});
我该怎么办? 谢谢
答案 0 :(得分:2)
将data
创建为单独的变量
var data = {code: foo};
data[name] = value;
$.ajax({
url: foo.php,
data: data,
type: 'POST',
success: function(data){}
});