2011-05-06 53 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