我编写了一个脚本,用于将json
数据传递给变量并在传递数据之后。在内部html页面中,我收集了所有数据,例如chapterid
,questionid
...等
$('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 & amp; &
amp; 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);
}
}
});
});
});
<div id="questionsNo_1_1">
<p class="text"><input class="option_1" id="ques11" name="radiobutton"
type="checkbox" value="Yes"></p>
<p class="text"><input class="option_2" id="ques12" name="radiobutton"
type="checkbox" value="No"></p>
</div>
<div id="questionsNo_1_2">
<p class="text"><input class="option_1" id="ques13" name="radiobutton"
type="checkbox" value="No"></p>
<p class="text"><input class="option_2" id="ques14" name="radiobutton"
type="checkbox" value="Yes"></p>
</div>
根据脚本,我只能在两个问题中检查ckeckboxes
option_1
。
我只想根据check box
数据选择json
。
为此,我已经尝试过这种情况,如果条件我又增加了一个条件。
if (
assessmentDataObj[key].chapter_id == fndChapterId & amp; & amp; assessmentDataObj[key].question_id == fndQuestionId & amp; $amp; assessmentDataObj[key].option_id == fndOptionId) {
为了获得fndOptionId
值,我写了如下语句:
var fndOptionId = $(this).attr('class').split('_')[1];
这两个问题得到了Option_1
。
checkboxes
如何根据json数据自动选择。
答案 0 :(得分:1)
您可以简化您的javascript功能:
$('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');
});
});