如何使用变量作为json属性? [英] How to use a variable as a json atribute?
本文介绍了如何使用变量作为json属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我在一个通用函数中工作,该函数根据选择的内容发送ajax请求:
Hello im working in a generic function that sends an ajax request according with the selection made:
$('#selectAcao, #selectAno').change(function(){
var sthis = $(this);
var sthisTipo = sthis.attr('rel');
var sthisName = sthis.attr('name');
var params = {
"tipo": sthisTipo,
sthisName : sthis.children('option:selected').val(),
"atualiza" : true
}
$.atualizaSelect(params);
});
我想要做的就是将"sthisName"变量作为"param"中的属性传递:
All I want is pass a "sthisName" variable as a property in "param":
var params = {
"tipo": sthisTipo,
sthisName : sthis.children('option:selected').val(),
"atualiza" : true
}
我该怎么做?
推荐答案
那不是JSON. JSON是一种数据序列化格式.您拥有的是一个JavaScript对象文字.
That isn't JSON. JSON is a data serialisation format. What you have is a JavaScript object literal.
无法在创建JavaScript对象时使用变量来定义属性名称.
There is no way to define a property name using a variable at creation time for a JavaScript object.
您必须先创建对象,然后添加属性.
You have to create the object first, and then add the property.
var myObject = {};
myObject[string_containing_property_name] = some_value;
这篇关于如何使用变量作为json属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文