var d = dialog({contentType:'load', skin:'bk-popup'}); var survey_id, q_info, indexArr = [], answerInfo = [], allAnswer = {}, pre_info; $(function(){ survey_id = localStorage.getItem("survey_id"); getQuestionInfo(1); bindEvents(); }); function getQuestionInfo(sort){ d.show(); var url = "/patient/questionnaire/getQuestions", params = { id: survey_id, sort: sort }; sendPost(url, params, 'JSON', 'GET', errorHandler, successHandler); } function errorHandler(res){ d.close(); if(res.msg){ dialog({contentType:'tipsbox', skin:'bk-popup' , content:res.msg,bottom:true}).show(); }else{ dialog({contentType:'tipsbox', skin:'bk-popup' , content:'数据加载失败!',bottom:true}).show(); } } function successHandler(res){ if(res.status == 200){ d.close(); var data = res.data; q_info = res.data; //计算进度比例 = 已完成题/(未完成题+已完成题) var noAns = parseInt(data.totalNum) - parseInt(data.sort)+1, ans = answerInfo.length, percent = parseInt((ans/(ans + noAns)).toFixed(2)*100); indexArr.push(data.sort); if(data.sort == 1){ //如果只有一题的话,直接显示提交按钮 if(data.totalNum == 1){ $(".group1").hide(); $(".group2").hide(); $(".group3").hide(); $(".group4").show(); }else{ $(".group1").show(); $(".group2").hide(); $(".group3").hide(); $(".group4").hide(); } } else if(data.sort > 1 && data.sort!=data.totalNum){ $(".group1").hide(); $(".group2").show(); $(".group3").hide(); $(".group4").hide(); }else{ $(".group1").hide(); $(".group2").hide(); $(".group3").show(); $(".group4").hide(); } $(".percent").text(percent+"%"); $(".progress").css("width", percent+"%"); if(data.type !=2){ for(i=0; i= 0){ nextCode = q_info.nextQuestion; } //必填情况判断 if(q_info.isRequired){ if(q_info.type !=2){ if(len == 0){ dialog({ contentType:'tipsbox', skin:'bk-popup' , content:'此题为必答题,请按要求作答' }).show(); return false; } }else{ if($.trim(text).length == 0){ dialog({ contentType:'tipsbox', skin:'bk-popup' , content:'此题为必答题,请按要求作答' }).show(); return false; } } } if(len > 0){ if(q_info.type == "1"){ //多选题 if(q_info.minNum && len < q_info.minNum){ dialog({ contentType:'tipsbox', skin:'bk-popup' , content:'此题要求最少选择'+q_info.minNum+"项,请按要求作答" }).show(); return false; }else if(q_info.maxNum && len > q_info.maxNum){ dialog({ contentType:'tipsbox', skin:'bk-popup' , content:'此题要求最多选择'+q_info.maxNum+"项,请按要求作答" }).show(); return false; } } obj.qstCode = q_info.qstCode; obj.sort = q_info.sort; obj.type = q_info.type; obj.options = []; for(i=0; i= 0){ nextCode = jsonData.nextQuestion } } }else{ //文本框内容 nextCode = q_info.nextCode || (q_info.sort+1); obj.qstCode = q_info.qstCode; obj.sort = q_info.sort; obj.type = q_info.type; obj.content = $.trim(text); } //将答题信息保存到storage中 answerInfo.push(obj); allAnswer[q_info.sort] = obj; pre_info = undefined; //发送请求获取下一题的内容 if(isNaN(parseInt(nextCode))){ nextCode = q_info.sort+1 } return nextCode; //hard code 下一题的值 // return (q_info.sort +1); } function bindEvents(){ $(".next_btn").on("click", function(){ var nextCode = validate(); if(nextCode){ getQuestionInfo(nextCode); } if(parseInt(nextCode) == 0){ submitAnswer(); } }); $(".pre_btn").on("click", function(){ //获取上一题的内容 pre_info = answerInfo[answerInfo.length-1]; answerInfo.pop(); getQuestionInfo(pre_info.sort); }); $(".submit_btn").on("click", function(){ var result = validate(); if(result){ submitAnswer(); } }); } function submitAnswer(){ dialog({ content: "是否确认提交问卷?", okValue: "立即提交", ok: function(){ d.show(); var url = "/patient/questionnaire/saveAnswer", params = { surveyCode: survey_id, questions: answerInfo }; sendPost(url, {jsonData: JSON.stringify(params)}, 'JSON', 'POST', function(res){ d.close(); if(res.msg){ dialog({contentType:'tipsbox', skin:'bk-popup' , content:res.msg,bottom:true}).show(); }else{ dialog({contentType:'tipsbox', skin:'bk-popup' , content:'数据提交失败!',bottom:true}).show(); } }, function(res){ if(res.status == 200){ dialog({contentType:'tipsbox', skin:'bk-popup' , content: "已提交成功,感谢您的参与",bottom:true}).show(); wx.closeWindow(); }else{ dialog({contentType:'tipsbox', skin:'bk-popup' , content:res.msg,bottom:true}).show(); } }); }, cancelValue: "我再看看", cancel: function(){ answerInfo.pop(); } }).showModal(); }