2014-04-03 23 views
0

我怎樣才能把變量轉換爲JSON 見例如需要把變量轉換成目標

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

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

    percent: 10, 
    speed: 'slow' 
}) 

我需要的不是一個數字「10」,我需要把可變測試 我如何能做到這一點,請大家幫幫我

+4

你試過用'test'代替'10'嗎? – adeneo

+2

另外,這不是json,這些是javascript對象文字。 –

回答

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

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

     percent: your_var , 
     speed: 'slow' 
    }) 
+0

這是工作,非常感謝Marc – user3127698

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格式,如果是的話,必須告訴我們更多。

+0

*我看你已經刪除了對json的引用現在的問題:P –

+0

嗨 我需要顯示一些我的計算作爲溫度計 – user3127698

+0

,但是當我在函數中插入我的計算時,然後它停止在這裏工作 – user3127698