var holders; var answers; /** * 问卷填写 */ var QM = { answer : { init : function(holderjson, answerjson) { holders = $.parseJSON(holderjson); if (holders == null) { holders = new Array(); } var $maindiv = $("#maiDiv"); $(holders).each(function(a) { var holder = holders[a]; var $div = $("
"); var $topic = QM.answer._createDiv(holder); $div.append($topic); $maindiv.append($div); $maindiv.append("
"); $maindiv.append("
"); }); answers = $.parseJSON(answerjson); if (answers) { for (var i = 0; i < answers.length; i++) { var answer = answers[i]; switch (answer.type) { case "radio": { if (answer.options) { for (var b = 0; b < answer.options.length; b++) { var option = answer.options[b]; var $aa = $("#" + answer.id).find( "input[name='" + answer.id + "'][value='" + option.name + "'] "); $aa.attr("checked", "checked"); var $bb = $("#" + answer.id).find( "#" + answer.id + option.name); $bb.val(option.explains); } } break; } case "check": { if (answer.options) { for (var b = 0; b < answer.options.length; b++) { var option = answer.options[b]; var $aa = $("#" + answer.id).find( "input[name='" + answer.id + "'][value='" + option.name + "'] "); $aa.attr("checked", "checked"); var $bb = $("#" + answer.id).find( "#" + answer.id + option.name); $bb.val(option.explains); } } break; } } } } var $completeB = $("#completeB"); $completeB.click(function() { QM.answer.encodeJson(holderjson); }); }, /** * 初始化试题 * * @param holder * @returns */ _createDiv : function(holder) { var id = holder.id; var type = holder.type; var number = holders.indexOf(holder); var topic = holder.topic; var headCount=0; for(var a=0; a"); $titleDiv = $("
"); }else{ var title = topic; $div = $("
"); $titleDiv = $("
"); } $titleDiv.text(title); // 是否必填 if (holder.isWill) { var $will = $("*"); $will.css("color", "red"); $titleDiv.prepend($will); } if (type.indexOf("test") >= 0) { var $code = $("(分数:" + holder.code + ")"); $titleDiv.append($code) } $div.append($titleDiv); // 内容 switch (type) { case "radio": { var options = holder.options; var order = holder.order; if (!order) { order = 1; } var $radioTable = $("
"); if (options != null) { for (var i = 0; i < options.length; i++) { var option = options[i]; if (i % order == 0) { $radioTable.append(""); } var $tr = $radioTable.find("tr:last"); var name = option.name; var $radioDiv = $(""); // 添加选项 var $radio = $(""); $radioDiv.append($radio); // 是否含有图片 if(holder.isPic && option.pic != ""){ var $pic = $('' +''); $radioDiv.append($pic); } $radioDiv.append(option.name); var $text = null; var $span = null; // 是否填空 if (option.isText) { var $text = $(""); $radioDiv.append($text); if (option.isWill) { var $span = $("*"); $radioDiv.append($span); } if (option.selected) { $text.show(); if (option.isWill) { $span.show(); } }else{ $text.hide(); if (option.isWill) { $span.hide(); } } } // 添加默认值 if (option.selected) { $radio.attr("checked", "checked"); } $radio.change(function() { var $radios = $("input[name='" + id + "'][type='radio']"); $radios.each(function(a) { var o = options[a]; o.selected = this.checked; if(this.checked){ $("input[name='"+id+"'][id='"+o.name+"']").show(); $("span[name='"+id+"'][id='"+o.name+"_span"+"']").show(); }else{ $("input[name='"+id+"'][id='"+o.name+"']").hide(); $("span[name='"+id+"'][id='"+o.name+"_span"+"']").hide(); } }) }); $tr.append($radioDiv); } $div.append($radioTable); } break; } case "check": { var options = holder.options; var order = holder.order; if (!order) { order = 1; } var options = holder.options; if (options != null) { var $checkTable = $("
"); for (var i = 0; i < options.length; i++) { var option = options[i]; var name = option.name; if (i % order == 0) { $checkTable.append(""); } var $tr = $checkTable.find("tr:last"); var $checkDiv = $(""); // 添加选项 var $check = $(""); // 添加默认值 if (option.selected) { $check.attr("checked", "checked"); } $checkDiv.append($check); // 是否含有图片 if(holder.isPic && option.pic != ""){ var $pic = $('' +''); $checkDiv.append($pic); } $checkDiv.append(option.name); $check.change(function() { var $checks = $("input[name='" + id + "'][type='checkbox']"); $checks.each(function(a) { var o = options[a]; o.selected = this.checked; //由选中状态改变文本框是否出现 if(this.checked){ $("input[name='"+id+"'][id='"+o.name+"']").show(); $("span[name='"+id+"'][id='"+o.name+"_span"+"']").show(); }else{ $("input[name='"+id+"'][id='"+o.name+"']").hide(); $("span[name='"+id+"'][id='"+o.name+"_span"+"']").hide(); } }) }); // 是否填空 if (option.isText) { var $text = $(""); $checkDiv.append($text); if (option.isWill) { var $span = $("*"); $checkDiv.append($span); } if (option.selected) { $text.show(); if (option.isWill) { $span.show(); } }else{ $text.hide(); if (option.isWill) { $span.hide(); } } } $tr.append($checkDiv); } $div.append($checkTable); } break; } case "input": { var $inputDiv = $("
"); var $input = $("