var question = function (entry) { entry = entry || {}; var rightAnswerArray = new Array(); var WordSerial = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]; this.test_question_Id = entry.test_question_Id || 0; this.base_question_type_id = entry.base_question_type_id; this.content = entry.content; this.question_file_id = entry.question_file_id; this.answers = entry.answers; this.note = entry.note || ""; this.test_question_libary_id = entry.test_question_libary_id; this.score = entry.score; this.difficulty_degree = entry.difficulty_degree; this.is_vaild = entry.is_vaild; this.libaryName = entry.libaryName; this.question_provid_answer = entry.question_provid_answer; this.TypeName = entry.TypeName; this.getEntry = function () { this.base_question_type_id = $("[id$=QuestionType]").combobox("getValue"); this.content = $('[id$=content]').val(); this.question_file_id = $('#questionUpload').next('#iptSingleFile').val(); if (this.question_file_id == "null" || this.question_file_id == "") this.question_file_id = 0; this.note = $('[id$=note]').val(); this.test_question_libary_id = $("[id$=txt_libary]").val(); this.score = $('[id$=score]').val(); this.difficulty_degree = $("[id$=difficulty_degree]").combobox("getValue"); this.is_vaild = $("input[id*='is_vaild']:checked").val(); this.test_question_Id = $("[id$=test_question_Id]").val(); var questionType = $("[id$=QuestionType]").combobox("getText"); this.question_provid_answer = getQuestionOption(questionType); this.answers = rightAnswerArray.join("|"); } this.setEntry = function () { $("[id$=QuestionType]").combobox("setValue", this.base_question_type_id); $("[id$=QuestionType]").combobox("setText", this.TypeName); $('[id$=content]').val(this.content); $('[id$=questionUpload]').next('#iptSingleFile').val(this.question_file_id); $('[id$=note]').val(this.note); $("[id$=txt_libary]").val(this.test_question_libary_id); $('[id$=txtScore]').val(this.score); $("[id$=difficulty_degree]").combobox("setValue", this.difficulty_degree); $("input[id*='is_vaild'][value=" + this.is_vaild + "]").attr("checked", true); $('[id$=lblLibaryName]').html(this.libaryName); rightAnswerArray = this.answers.split(','); showOptions(this.question_provid_answer); createImg(this.question_file_id, $('[id$=questionUpload]')); } this.validateEntry = function () { $(".errorInput").removeClass("errorInput"); if (isNull(this.test_question_libary_id)) { alert("请选择题库"); $('[id$=ddlLibary]').addClass("errorInput"); return false; } if (isNull(this.base_question_type_id)) { alert("请选择试题类型"); $('[id$=ddlQuestionType]').addClass("errorInput"); return false; } if (isNull(this.content)) { alert("题干不能为空"); $('[id$=txtContent]').addClass("errorInput"); return false; } if (isNull(this.difficulty_degree)) { alert("请选择难度系数"); $('[id$=ddlDifficulty]').addClass("errorInput"); return false; } if (isNull(this.score)) { alert("缺省分数不能为空"); $('[id$=txtScore]').addClass("errorInput"); return false; } var questionType = $("[id$=QuestionType]").combobox("getText"); if (questionType == "单选题" || questionType == "多选题") { if (this.question_provid_answer.length == 0) { alert(questionType + "必须有选项"); return false; } if (questionType == "单选题" && rightAnswerArray.length != 1) { alert(questionType + "只能有一个正确答案"); return false; } if (questionType == "多选题" && rightAnswerArray.length == 0) { alert(questionType + "必须设置正确答案"); return false; } } if (!checkOptionIsNull(this.question_provid_answer)) { alert("选项内容不能为空"); return false; } return true; } function checkOptionIsNull(providAnswer) { var isFlag = true; $(providAnswer).each(function (i, ans) { if (isNull(ans.answer_name)) { isFlag = false; return false; } }); return isFlag; } function showOptions(providAnswer) { $('.trOption').find('div').hide(); var questionType = $("[id$=QuestionType]").combobox("getText"); if (questionType == "单选题") { return showSingleOption(providAnswer); } else if (questionType == "多选题") { return showMultipleOption(providAnswer); } else if (questionType == "填空题") { return showFillOption(providAnswer); } else if (questionType == "是非题") { return showJudgeOption(providAnswer); } else if (questionType == "简答题") { return showShortOption(providAnswer); } } function showSingleOption(providAnswer) { var reg1 = /^.{2}/; $(providAnswer).each(function (i, ans) { var tpl = $('#optionTpl').find('#trSingleTpl').clone(); tpl.find("[name$=txtContext]").val(ans.answer_name.replace(reg1, "")); tpl.find("[id$=iptAnswerID]").val(ans.provid_answer_id); tpl.find('[id$=iptSingleFile]').val(ans.file_id); createImg(ans.file_id, tpl.find('[id$=singleUpload]')); var ckbRight; if (jQuery.inArray(ans.provid_answer_id.toString(), rightAnswerArray) > -1) { ckbRight = tpl.find("[id$=rdIsRight]"); } tpl.insertBefore($('[id$=trSingle]').last()); $(ckbRight).attr("checked", true); //兼容IE6啊 }); $('#divSingle').show(); } function showMultipleOption(providAnswer) { var reg1 = /^.{2}/; $(providAnswer).each(function (i, ans) { var tpl = $('#optionTpl').find('#trMultipleTpl').clone(); tpl.find("[name$=txtContext]").val(ans.answer_name.replace(reg1, "")); tpl.find("[id$=iptAnswerID]").val(ans.provid_answer_id); tpl.find('[id$=iptSingleFile]').val(ans.file_id); createImg(ans.file_id, tpl.find('[id$=singleUpload]')); var ckbRight; if (jQuery.inArray(ans.provid_answer_id.toString(), rightAnswerArray) > -1) { ckbRight = tpl.find("[id$=ckbIsRight]"); } tpl.insertBefore($('[id$=trMultiple]').last()); $(ckbRight).attr("checked", true); //兼容IE6啊 }); $('#divMultiple').show(); } function showFillOption(providAnswer) { var reg1 = /^.{2}/; $(providAnswer).each(function (i, ans) { var tpl = $('#optionTpl').find('#trFillTpl').clone(); tpl.find("[name$=txtContext]").val(ans.answer_name); tpl.find("[id$=iptAnswerID]").val(ans.provid_answer_id); tpl.find('[id$=iptSingleFile]').val(ans.file_id); createImg(ans.file_id, tpl.find('[id$=fillUpload]')); tpl.insertBefore($('[id$=trFill]').last()); }); $('#divFill').show(); } function showJudgeOption(providAnswer) { $(providAnswer).each(function (i, ans) { if (jQuery.inArray(ans.provid_answer_id.toString(), rightAnswerArray) > -1) { $("input[name='rdJudgeOption'][value=" + ans.answer_name + "]").attr("checked", true); } }); $('#divJudge').show(); } function showShortOption(providAnswer) { $(providAnswer).each(function (i, ans) { $('#divShort').find("[name='txtShortAnswer']").val(ans.answer_name); }); $('#divShort').show(); } function getQuestionOption(questionType) { if (questionType == "单选题") { return SingleOption(); } else if (questionType == "多选题") { return MultipleOption(); } else if (questionType == "填空题") { return FillOption(); } else if (questionType == "是非题") { return JudgeOption(); } else if (questionType == "简答题") { return ShortOption(); } } function SingleOption() { var questionOptions = new Array(); $("#tbSingle").find("tr").filter(function () { return $(this).find('[name$=txtContext]').length > 0; }).each(function (i, j) { var item = $(j); var answer_name = item.find('[name$=txtContext]').val(); if (answer_name != undefined && answer_name != "") { answer_name = WordSerial[i] + "、" + answer_name; } var isRight = item.find('#rdIsRight').attr('checked'); var iptAnswerID = item.find("[id='iptAnswerID']").val() || 0; if (isRight) { rightAnswerArray.push(answer_name); } var file_id = item.find('#iptSingleFile').val() || null; questionOptions.push({ provid_answer_id: iptAnswerID, answer_name: answer_name, isRight: isRight, file_id: file_id }); }); return questionOptions; } function MultipleOption() { var questionOptions = new Array(); $("#tbMultiple").find("tr").filter(function () { return $(this).find('[name$=txtContext]').length > 0; }).each(function (i, j) { var item = $(j); var answer_name = item.find('[name$=txtContext]').val(); if (answer_name != undefined && answer_name != "") { answer_name = WordSerial[i] + "、" + answer_name; } var isRight = item.find('#ckbIsRight').attr('checked'); var iptAnswerID = item.find("[id='iptAnswerID']").val() || 0; if (isRight) { rightAnswerArray.push(answer_name); } var file_id = item.find('#iptSingleFile').val() || null; questionOptions.push({ provid_answer_id: iptAnswerID, answer_name: answer_name, isRight: isRight, file_id: file_id }); }); return questionOptions; } function FillOption() { var questionOptions = new Array(); $("#tbFill").find("tr").filter(function () { return $(this).find('[name$=txtContext]').length > 0; }).each(function (i, j) { var item = $(j); var answer_name = item.find('[name$=txtContext]').val(); var isRight = item.find('#ckbIsRight').attr('checked'); var file_id = item.find('#iptSingleFile').val() || null; var iptAnswerID = item.find("[id='iptAnswerID']").val() || 0; rightAnswerArray.push(answer_name); questionOptions.push({ provid_answer_id: iptAnswerID, answer_name: answer_name, isRight: true, file_id: file_id }); }); return questionOptions; } function JudgeOption() { var questionOptions = new Array(); var rightAnswer = $('#divJudge').find("input[name='rdJudgeOption']:checked").val(); questionOptions.push({ answer_name: "正确", isRight: rightAnswer == "正确" }); questionOptions.push({ answer_name: "错误", isRight: rightAnswer == "错误" }); rightAnswerArray.push(rightAnswer); return questionOptions; } function ShortOption() { var questionOptions = new Array(); var answer = $('#divShort').find("[name='txtShortAnswer']").val(); var iptAnswerID = $('#divShort').find("[id='iptAnswerID']").val() || 0; rightAnswerArray.push(answer); questionOptions.push({ provid_answer_id: iptAnswerID, answer_name: answer, isRight: true }); return questionOptions; } } $(function () { if (typeof queLogic == "undefined") showQuestionOption(); $('[id$=ddlQuestionType]').change(function () { showQuestionOption(); }) $('[id$=btnSave]').click(function () { var queLogic = new question(); queLogic.getEntry(); if (!queLogic.validateEntry()) { return false; } var inventoryJSONTextAgain = JSON.stringify(queLogic, null, 3); $('#question_provid_answer').val(JSON.stringify(queLogic.question_provid_answer)) $('[id$=ipt_entry]').val(inventoryJSONTextAgain); $(document.forms[0]).submit(); }) $("textarea[maxlength]").bind('input propertychange', function () { var maxLength = $(this).attr('maxlength'); if ($(this).val().length > maxLength) { $(this).val($(this).val().substring(0, maxLength)); alert("限字数为300"); } }) }); var uploadSender; function UploadFileBefore(sender){ uploadSender=sender; UploadFile(); } function GetUploadFileMessage(file_id, Url){ $(uploadSender).parent().find('#iptSingleFile').val(file_id); $(uploadSender).parent().find('img').next("a").remove(); $(uploadSender).parent().find('img').remove(); createImg(file_id, uploadSender); $(uploadSender).parent().find('.singleUpload').hide(); } function createImg(file_id,sender){ if(isNull(file_id)||isNull(sender)) return; $(sender).parent().find('.singleUpload').hide(); $(sender).before("

删除
"); //$(sender).before("
删除"); } function deleteImg(center) { $(center).parent().find('#iptSingleFile').val(""); $(center).parent().find('img').remove(); $(center).parent().find('br').remove(); $(center).parent().parent().find('.singleUpload').show(); $(center).remove(); } function isNull(value) { return value == undefined || value == null || value == ""; } function AddSingleRowHandler(){ AddSingleRow('trSingle'); } function AddFillRowHandler(){ AddSingleRow('trFill'); } function AddMultipleRowHandler() { AddSingleRow('trMultiple'); } function AddSingleRow(target){ $('#optionTpl').find('#'+target+'Tpl').clone().insertBefore($('[id$='+target+']').last()); } function delRow(sender){ $(sender).parent().parent().remove(); } function showQuestionOption(index) { if (index == undefined) return; $('.trOption').find('div').hide(); var data = $("[id$=QuestionType]").combobox("getData"); var selQuestionType = data[index-1].Name; if (selQuestionType == "单选题" ) { var optionCount = $('#divSingle').find('[id=trSingleTpl]').length; for (var i = 0; i < 4 - optionCount; i++) { AddSingleRow('trSingle'); } $('#divSingle').show(); $('#spAnswerTip').show(); } else if (selQuestionType == "多选题") { var optionCount = $('#divMultiple').find('[id=trMultipleTpl]').length; for (var i = 0; i < 4 - optionCount; i++) { AddSingleRow('trMultiple'); } $('#divMultiple').show(); $('#spAnswerTip').show(); } else if (selQuestionType == "填空题") { if ($('#divFill').find('#trFillTpl').length == 0) AddSingleRow('trFill'); $('#divFill').show(); $('#spAnswerTip').show(); } else if (selQuestionType == "是非题") { $('#divJudge').show(); $('#spAnswerTip').hide(); } else if (selQuestionType == "简答题") { $('#divShort').show(); $('#spAnswerTip').hide(); } }