2011-05-06 52 views
0

我想用AND條件創建一個Json對象(我​​不知道它是否可能)。我把目前爲止我未完成的代碼。Json對象條件

var parameters = { 
       Title : "hello", 
       Text : "world", 
       Question : //if two checkboxes are checked then true else false. 
} 

關於兩個複選框選中我想使用這個:

$('.checkbox1').is(':checked') && $('.checkbox2').is(':checked') 

什麼,我想,以避免被放了,如果像

//if checkboxes are checked 
//{ 
//create the json object of type 1 
//} 
//else 
//{ 
//create the json object of type 2 
//} 

這可能嗎?

+1

您剛剛嘗試過嗎?順便說一句。這是**不是** JSON對象。這是一個普通的JavaScript對象。 – 2011-05-06 10:07:56

回答

1

是的,一個布爾值將是確定:

var parameters = { 
      Title : "hello", 
      Text : "world", 
      Question : $('.checkbox1').is(':checked') && $('.checkbox2').is(':checked') 
} 

這將創建一個常規的JavaScript對象,您可以操控像任何其他物體,例如,

parameters.Question = <newTrueOrFalseToSet>; 

甚至:

parameters["Question"] = <newTrueOrFalseToSet>; 

Acc或生成JSON definition,生成的JSON應如下所示:

{ 
    "Title": "hello", 
    "Text": "world", 
    "Question": true 
} 
+0

非常感謝。非常好! – Jose3d 2011-05-06 10:09:11