2012-10-24 42 views
1

行,所以我有這樣一段代碼重複?變化值使用jQuery

+1

爲什麼你會需要遍歷:該值可以如下訪問? 'if(「el」中的isSel)el [「isSel」] = false;' – adeneo

+0

這裏沒有任何JSON。你只需要標準的舊Javascript對象。 – James

回答

5

將其更改爲

var el = {a:"b","isSel":true}; 
$.each(el,function(k,v){ 
    if(k=="isSel"){ 
    el[k]=false // <= this sets the property named k of el 
    } 
}) 
console.log(el); 

請注意,如果你只是想更改名爲EL的"isSel"的財產,你不必重複:你可以簡單地做

el["isSel"] = false; 

el.isSel = false; 
+0

非常感謝! – climboid

0

沒有必要循環。

el.isSel = false; 

el["isSel"] = false; 
+0

是的,我實際上有很多嵌套屬性,我處理的JSON有點複雜,所以必須向內循環 – climboid