memories forever memories forever - 4 months ago 7
JSON Question

how to check the check boxes with retrieved data

i have written script that to pass json data to a variable and after passing data .In the inner html page i collect all the data like chapterid ,questionid...etc

$('div[id^="questionsNo_"]').ready(function () {
var assessmentDataObj = [{"id":"1","user_id":"1","book_id":"64","chapter_id":"1","question_type":"","question_id":"1","option_id":"1","mark":"0","notes":"","matchOption":"","dragValues":"","added_time":"1469595022"},{"id":"2","user_id":"1","book_id":"64","chapter_id":"1","question_type":"","question_id":"2","option_id":"2","mark":"0","notes":"","matchOption":"","dragValues":"","added_time":"1469622667"}];

$("div[id^='questionsNo_']").each(function () {
var fndDetails = $(this).attr('id').split('_');
var fndChapterId = fndDetails[1];
var fndQuestionId = fndDetails[2];
$.each(assessmentDataObj, function (key, value) {
if (assessmentDataObj[key].chapter_id == fndChapterId && assessmentDataObj[key].question_id == fndQuestionId) {
$('div[id^="questionsNo_' + assessmentDataObj[key].chapter_id + "_" + assessmentDataObj[key].question_id +'"]'+
'p input.option_' + assessmentDataObj[key].option_id).attr('checked', 'checked');
var a = assessmentDataObj[key].option_id;
if(assessmentDataObj[key].question_id == 1){
$('.option_' + a).attr('checked',true);
}
}
});
});
});


and my html content is

<div id="questionsNo_1_1">
<p class="text"><input type="checkbox" value="Yes" id="ques11" name="radiobutton" class="option_1" /></p>
<p class="text"><input type="checkbox" value="No" id="ques12" name="radiobutton" class="option_2" /></p>
</div>
<div id="questionsNo_1_2">
<p class="text"><input type="checkbox" value="No" id="ques13" name="radiobutton" class="option_1" /></p>
<p class="text"><input type="checkbox" value="Yes" id="ques14" name="radiobutton" class="option_2" /></p>
</div>


according to the script i am able to check the ckeckboxes which is having option_1 only in two questions.i want only the check box should select according to the json data.
For that i have tried in this way in if condition i have added one more condition .

if (assessmentDataObj[key].chapter_id == fndChapterId &amp;&amp; assessmentDataObj[key].question_id == fndQuestionId &amp;$amp; assessmentDataObj[key].option_id == fndOptionId) {


For getting fndOptionId value i write statement like

var fndOptionId = $(this).attr('class').split('_')[1];


This was getting Option_1,in the two questions.

How do the checkboxes should select automatically according to the json data.help me

Answer

You could simplify your javascript function:

   $('div[id^="questionsNo_"]').ready(function () {
    var assessmentDataObj = [{"id":"1","user_id":"1","book_id":"64","chapter_id":"1","question_type":"","question_id":"1","option_id":"1","mark":"0","notes":"","matchOption":"","dragValues":"","added_time":"1469595022"},{"id":"2","user_id":"1","book_id":"64","chapter_id":"1","question_type":"","question_id":"2","option_id":"2","mark":"0","notes":"","matchOption":"","dragValues":"","added_time":"1469622667"}]; 
 $.each(assessmentDataObj, function (key, value) 
    {
    $("#questionsNo_1_" + assessmentDataObj[key].question_id).find('p input.option_' + assessmentDataObj[key].option_id).attr('checked', 'checked');         
    });
    });