开发者

Json object condition

开发者 https://www.devze.com 2023-03-04 14:33 出处:网络
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.

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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消