需要将变量放入对象中

时间:2014-04-03 20:41:48

标签: javascript

我如何将变量放入json 见例子

$('.thermometer-noconfig').thermometer({

    percent: 10,   
    speed: 'slow'
})
/* thermometers with config */
$('.thermometer-config').thermometer({

    percent: 10, 
    speed: 'slow'
})

我需要代替数字“10”,我需要进行变量测试 我怎么能这样做,请帮助我

2 个答案:

答案 0 :(得分:1)

your_var = 'whatever'; 
$('.thermometer-noconfig').thermometer({

    percent: 10,   
      speed: 'slow'
    })
    /* thermometers with config */
    $('.thermometer-config').thermometer({

      percent: your_var , 
      speed: 'slow'
    })

答案 1 :(得分:0)

var myThing = { percent: 10, speed: 'slow' };

mything.percent="test";

var myJsonThing = JSON.stringify(myThing);

现在我不知道温度计是什么'是/或它需要什么 - 但你可以通过其中任何一个:

$('.thermometer-config').thermometer(myThing);

或者如果它需要JSON格式:

$('.thermometer-config').thermometer(myJsonThing);

JSON.stringyfy() myThing的输出是

{ "percent": "test", "speed": "slow" }

,但由于您没有通过http将此数据传递给服务,因此我不知道您要求使用json格式的原因,如果有,请告诉我们更多信息。