Json object condition
i would like to create a Json object using an AND condition (i dont know if its possible). I put the unfinished code that i've by the moment.
var parameters = {
Title : "hello",
Text : "world",
Question : //if two checkboxes are checked then true else false.
}
About the two checkboxes checked i was thinking to use this:
$('.checkbox1').is(':checked') && $('.checkbox2').is(':checked')
What i would like to avoid is to put an if like
//if checkboxes are checked
//{
//create the json object of type 1
//}
//else
//{
//create the json obje开发者_高级运维ct of type 2
//}
Is this possible?
Yes, a boolean would be ok:
var parameters = {
Title : "hello",
Text : "world",
Question : $('.checkbox1').is(':checked') && $('.checkbox2').is(':checked')
}
This will create a regular JavaScript object which you can manipulate like any other object, e.g.,
parameters.Question = <newTrueOrFalseToSet>;
Or even:
parameters["Question"] = <newTrueOrFalseToSet>;
According to the JSON definition, the resulting JSON should look like:
{
"Title": "hello",
"Text": "world",
"Question": true
}
精彩评论