如何在jquery对象中设置变量的键?

时间:2013-09-06 12:45:50

标签: javascript jquery

如何将“nom_articole”键更改为 s 值?

        var s = document.getElementById("myvalue").textContent;

        var obj = {
            url: "http://localhost:53698/mobile_ODataService.svc/",
            jsonp: true,
            errorHandler: function (error) {
                alert(error.message);
            },
            entities: {
                nom_articole/*I want here the s value in nom_articole place*/: { key: "id" },
            }
        };

2 个答案:

答案 0 :(得分:2)

您不能在对象文字中使用变量作为标识符。您可以在创建对象后添加另一个动态属性:

var s = document.getElementById("myvalue").textContent;

var obj = {
  url: "http://localhost:53698/mobile_ODataService.svc/",
  jsonp: true,
  errorHandler: function (error) {
     alert(error.message);
  },
  entities : {}
};
obj.entities[s] = { key: "id" };

答案 1 :(得分:0)

创建对象后必须执行此操作。

var s = document.getElementById("myvalue").textContent;

var obj = {
    url: "http://localhost:53698/mobile_ODataService.svc/",
    jsonp: true,
    errorHandler: function (error) {
        alert(error.message);
    }
};
obj[s] = {
    nom_articole: { key: "id" },
};