暂无描述

questionnarire.js 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * 问卷调查
  3. * */
  4. var countyPicker;
  5. Zepto(function($) {
  6. //获取乡镇
  7. getCounty();
  8. //获取问卷
  9. getPagers();
  10. //绑定提交按钮
  11. $("#submit").on('tap', submitHandle);
  12. });
  13. //提交
  14. function submitHandle() {
  15. var telreg = /^\d{3,12}$/;
  16. var that = this;
  17. if(!$.trim($("#h_userName").val())) {
  18. mui.toast("请您输入姓名后再提交");
  19. return;
  20. }
  21. if(!$.trim($("#h_telphone").val())) {
  22. mui.toast("请您输入手机号码后再提交");
  23. return;
  24. }
  25. if($.trim($("#h_telphone").val())) {
  26. if(!telreg.test($.trim($("#h_telphone").val()))) {
  27. mui.toast("请您输入正确手机号码");
  28. return;
  29. }
  30. }
  31. if(!$.trim($("#h_county").val())) {
  32. mui.toast("请您选择乡镇后再提交");
  33. return;
  34. }
  35. var anArrs = getAnswers();
  36. $.ajax({
  37. type: "post",
  38. url: huayi.config.callcenter_url + 'WxLogin/AddPagers',
  39. async: true,
  40. beforeSend: function() {
  41. mui(that).button('loading');
  42. },
  43. data: {
  44. openid: helper.cookies.get("openid"), //string 是
  45. cusname: $('#h_userName').val(), //string 是 姓名
  46. telphone: $('#h_telphone').val(), //string 是 联系电话
  47. phone: $('#h_phone').val(), //string 否 固定电话
  48. countryid: $('#h_county').attr('data-index'), //int 否 乡镇
  49. address: $('#h_address').val(), //string 否 住址
  50. ans: anArrs, //string 否 答案,数组形式["15_20_单选选项内容","17_25|36|58_复选选项内容1|选项内容2|选项内容3","30_0_问答题"]*/
  51. },
  52. dataType: 'json',
  53. success: function(data) {
  54. mui(that).button('reset');
  55. if(data.state.toLowerCase() == "success") {
  56. mui.alert("提交成功", ' ', function() {
  57. //清空数据
  58. clean();
  59. //重新获取问卷
  60. getPagers();
  61. });
  62. }
  63. },
  64. error: function(textStatus) {
  65. mui(that).button('reset');
  66. mui.toast('网络繁忙,请稍后再试...');
  67. },
  68. complete: function(XMLHttpRequest, textStatus) {
  69. mui(that).button('reset');
  70. if(textStatus == 'timeout') {
  71. var xmlhttp = window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHttp");
  72. xmlhttp.abort();
  73. mui.toast('网络超时,请稍后再试...');
  74. }
  75. },
  76. });
  77. }
  78. //获取试题答案
  79. function getAnswers() {
  80. /*单选 quesid_radio.val_radio.text;
  81. 多选quesid_25|36|58_复选选项内容1|选项内容2|选项内容3
  82. 问答题quesid_0_问题.text*/
  83. var anArr = [];
  84. var anForm = $('#questionBody').find('.queForm');
  85. anForm.forEach(function(v, i) {
  86. var str = '';
  87. var inputType = $(v).find('input').attr('type');
  88. if(inputType == 'radio') { //单选
  89. //单选 quesid_radio.val_radio.text;
  90. var radioName = $(v).find('input').attr('name');
  91. var radioQid = radioName && radioName.split('_')[1];
  92. var radioVal = $('input[name=' + radioName + ']:checked').val();
  93. var radioTxt = $('input[name=' + radioName + ']:checked').prev().text();
  94. if(radioVal && radioTxt) {
  95. str = radioQid + '_' + radioVal + '_' + radioTxt;
  96. anArr.push(str);
  97. }
  98. } else if(inputType == 'checkbox') { //多选
  99. //多选quesid_25|36|58_复选选项内容1|选项内容2|选项内容3
  100. var checkboxName = $(v).find('input').attr('name');
  101. var checkboxQid = checkboxName && checkboxName.split('_')[1];
  102. var checkboxCheckedEl = $('input[name=' + checkboxName + ']:checked');
  103. if(checkboxCheckedEl && checkboxCheckedEl.length > 0) {
  104. var cVal = '';
  105. var cTxt = '';
  106. checkboxCheckedEl.forEach(function(val, j) {
  107. cVal += '|' + $(val).val();
  108. cTxt += '|' + $(val).prev().text();
  109. });
  110. cVal = cVal && cVal.substring(1);
  111. cTxt = cTxt && cTxt.substring(1);
  112. str = checkboxQid + '_' + cVal + '_' + cTxt;
  113. anArr.push(str);
  114. }
  115. }
  116. //多选
  117. if($(v).find('textarea')) {
  118. var textareaName = $(v).find('textarea').attr('name');
  119. var textareaQid = textareaName && textareaName.split('_')[1];
  120. var textareaVal = $(v).find('textarea').val();
  121. if(textareaVal) {
  122. str = textareaQid + '_' + 0 + '_' + textareaVal;
  123. anArr.push(str);
  124. }
  125. }
  126. });
  127. return anArr;
  128. }
  129. //获取问卷
  130. function getPagers() {
  131. $.ajax({
  132. type: "get",
  133. url: huayi.config.callcenter_url + 'WxLogin/GetPagers',
  134. async: true,
  135. data: {
  136. openid: helper.cookies.get("openid"),
  137. },
  138. success: function(data) {
  139. $('#questionBody').empty();
  140. if(document.getElementById('hy_loading')){
  141. document.getElementById('hy_loading').style.opacity = 0;
  142. document.getElementById('hy_loading').remove();
  143. }
  144. data = JSON.parse(data);
  145. if(data.state.toLowerCase() == "success") {
  146. var res = data.data;
  147. if(res) {
  148. $('#que_header_title').text(res.F_Title); //问卷标题
  149. $('#que_header_content').text(res.F_Remark); //问卷说明
  150. var questions = res.F_Questions; //问卷的试题
  151. //questype 2单选 3 多选 1问答
  152. if(questions && questions.length > 0) {
  153. //遍历问卷的试题
  154. for(var i = 0; i < questions.length; i++) {
  155. var questype = questions[i].questype; //试题类型
  156. var quesid = questions[i].quesid; //试题id
  157. var quesitems = questions[i].quesitems; //试题选项
  158. var html = '';
  159. //根据试题的类型渲染页面
  160. if(questype == 2) {
  161. html = '<div class="mui-card">' +
  162. '<div class="mui-card-header">' +
  163. '<div class="qus_title"><span class="qus_type qus_radio">单选题</span>' + questions[i].questitle + '</div>' +
  164. '</div>' +
  165. '<form class="mui-input-group queForm">'
  166. if(quesitems && quesitems.length > 0) {
  167. for(var j = 0; j < quesitems.length; j++) {
  168. html += '<div class="mui-input-row mui-radio mui-left">' +
  169. '<label>' + quesitems[j].itemname + '</label>' +
  170. '<input name="radio_' + quesid + '" type="radio" value="' + quesitems[j].itemid + '">' +
  171. '</div>'
  172. }
  173. }
  174. html += '</form></div>';
  175. } else if(questype == 3) {
  176. html = '<div class="mui-card">' +
  177. '<div class="mui-card-header">' +
  178. '<div class="qus_title">' +
  179. '<span class="qus_type qus_check">多选题</span>' + questions[i].questitle +
  180. '</div>' +
  181. '</div>' +
  182. '<form class="mui-input-group queForm">'
  183. if(quesitems && quesitems.length > 0) {
  184. for(var k = 0; k < quesitems.length; k++) {
  185. html += '<div class="mui-input-row mui-checkbox mui-left">' +
  186. '<label>' + quesitems[k].itemname + '</label>' +
  187. '<input name="checkbox_' + quesid + '" value="' + quesitems[k].itemid + '" type="checkbox">' +
  188. '</div>'
  189. }
  190. }
  191. html += '</form></div>';
  192. } else if(questype == 1) {
  193. html = '<div class="mui-card">' +
  194. '<div class="mui-card-header">' +
  195. '<div class="qus_title">' +
  196. '<span class="qus_type qus_answer">问答题</span>' + questions[i].questitle +
  197. '</div>' +
  198. '</div>' +
  199. '<form class="mui-input-group queForm">' +
  200. '<div class="mui-input-row h_content">' +
  201. '<textarea name="textarea_' + quesid + '" class="mui-input-clear" placeholder="请输入。。。"></textarea>' +
  202. '</div>' +
  203. '</form>' +
  204. '</div>';
  205. }
  206. $('#questionBody').append(html);
  207. }
  208. }
  209. }
  210. }
  211. }
  212. });
  213. }
  214. //获取乡镇
  215. function getCounty() {
  216. $.ajax({
  217. type: "get",
  218. url: huayi.config.callcenter_url + "WxLogin/GetAreaList",
  219. dataType: 'json',
  220. async: true,
  221. data: {
  222. openid: helper.cookies.get("openid"),
  223. },
  224. success: function(data) {
  225. if(data.state.toLowerCase() == "success") {
  226. var res = data.data;
  227. if(res.length > 0) {
  228. //下拉弹出
  229. (function($, doc) {
  230. $.init();
  231. $.ready(function() {
  232. var t_countyDatas = [{value: 0,text: '请选择乡镇'}];
  233. res.forEach(function(item, index) {
  234. t_countyDatas.push({
  235. value: item.F_DictionaryValueId,
  236. text: item.F_Name
  237. });
  238. });
  239. countyPicker = new $.PopPicker();
  240. countyPicker.setData(t_countyDatas);
  241. var showCountybtn = doc.getElementById('showCountyPicker');
  242. var countyRes = doc.getElementById('h_county');
  243. showCountybtn.addEventListener('tap', function(event) {
  244. countyPicker.show(function(items) {
  245. countyRes.value = items[0].text;
  246. countyRes.setAttribute("data-index", items[0].value);
  247. });
  248. }, false);
  249. });
  250. })(mui, document);
  251. }
  252. }
  253. }
  254. });
  255. }
  256. //清空数据
  257. function clean() {
  258. $('.mui-input-row').find('.mui-input-clear').val('');
  259. countyPicker.pickers[0].setSelectedValue(0, 200, function() {
  260. var sItems = countyPicker.getSelectedItems();
  261. $('#h_county').val('请选择乡镇');
  262. $('#h_county').attr('data-index', 0);
  263. });
  264. $('#questionBody').find('input').prop('checked',false);
  265. $('#questionBody').find('.mui-input-clear').val('');//问答题
  266. }