1、选中特定的单选按钮
function showDetail(content){$("input[name^='radio']").removeAttr("checked");for(var i=0;i<content.length;i++){$("#radio"+(i+1)+content.substr(i,1)).attr("checked","checked");}
}
2、手动添加问卷
javascript部分代码
1 $('.handleSaveBtn').click(function(){ 2 //判空,标题、有效期、题干、选项、分值 3 if ($("#title1").val() === '') { 4 alert("问卷标题不可为空"); 5 return; 6 } 7 if ($("#timeOut1").val() === '') { 8 alert("问卷有效期不可为空"); 9 return; 10 } 11 var flag=0; 12 $("input[name^='q']").each(function(){ 13 if($(this).val()===""){ 14 alert("题干、选项、分值等不可为空!"); 15 flag=1; 16 return false; 17 } 18 }); 19 if(flag===1){ 20 return; 21 } 22 //组成问卷questions部分 23 var obj=new Object(); 24 var questions=new Array(); 25 var totalScore=0,score1,score2,score3,score4; 26 for(var i=0;i<10;i++){ 27 var j=i+1; 28 var question=new Object(); 29 var type=$("#type"+j).val(); 30 question.type=type; 31 question.ordinal=j; 32 question.content=$("input[name='q"+j+"']").val(); 33 if(type==='SINGLE'){ 34 var choice1={"value":"A","content":$('input[name="q'+j+'c1"]').val(),"score":$('input[name="q'+j+'cm1"]').val()}; 35 var choice2={"value":"B","content":$('input[name="q'+j+'c2"]').val(),"score":$('input[name="q'+j+'cm2"]').val()}; 36 var choice3={"value":"C","content":$('input[name="q'+j+'c3"]').val(),"score":$('input[name="q'+j+'cm3"]').val()}; 37 var choice4={"value":"D","content":$('input[name="q'+j+'c4"]').val(),"score":$('input[name="q'+j+'cm4"]').val()}; 38 question.choices=[choice1,choice2,choice3,choice4]; 39 40 score1=$('input[name="q'+j+'cm1"]').val(); 41 score2=$('input[name="q'+j+'cm2"]').val(); 42 score3=$('input[name="q'+j+'cm3"]').val(); 43 score4=$('input[name="q'+j+'cm4"]').val(); 44 if(score1<score2){ 45 score1=score2; 46 } 47 if(score3<score4){ 48 score1=score2; 49 } 50 if(score1<score3){ 51 score1=score3; 52 } 53 }else if(type==='MULTIPLE'){ 54 var choice1={"value":"A","content":"是","score":$('input[name="q'+j+'cm1"]').val()}; 55 var choice2={"value":"B","content":"否","score":$('input[name="q'+j+'cm2"]').val()}; 56 question.choices=[choice1,choice2]; 57 58 score1=$('input[name="q'+j+'cm1"]').val(); 59 score2=$('input[name="q'+j+'cm2"]').val(); 60 if(score1<score2){ 61 score1=score2; 62 } 63 }else{ 64 var choice1={"value":"A","content":$('input[name="q'+j+'c1"]').val(),"score":$('input[name="q'+j+'cm1"]').val()}; 65 question.choices=[choice1]; 66 score1=$('input[name="q'+j+'cm1"]').val(); 67 } 68 totalScore+score1; 69 questions.push(question); 70 } 71 if(totalScore>100){ 72 alert("总分不能超过100!"); 73 return; 74 } 75 obj.questions=questions; 76 var json=JSON.stringify(obj); 77 78 var formData = new FormData(); 79 formData.append("title",$("#title1").val()); 80 formData.append("status",$("#status1").val()); 81 formData.append("timeOut",$("#timeOut1").val()); 82 formData.append("questions",json); 83 alert(json); 84 85 //提交 86 var url = 'cms/survey/add2'; 87 $.ajax({ 88 url: url, 89 data: formData, 90 processData: false, 91 contentType: false, 92 type: 'POST', 93 success: function (job) { 94 console.log(job); 95 if (job) { 96 alert("问卷添加成功!"); 97 location.reload(); 98 } else { 99 alert('问卷添加失败'); 100 } 101 } 102 }).fail(function () { 103 alert('问卷添加失败'); 104 }); 105 106 107 }); 108 </script>
其中第76行代码var json=JSON.stringify(obj);是将js对象转换为JSON对象。
java部分代码
1 /** 2 * 手动添加问卷 3 * 4 * @param req 5 * @param title 6 * @param ownerId 7 * @param ownerRealm 8 * @param type 9 * @param questions 10 * @param timeOut 11 * @return 12 */ 13 @POST 14 @Path("/add2") 15 @Consumes(MediaType.MULTIPART_FORM_DATA) 16 public Response addSurvey2(@Context HttpServletRequest req, 17 @FormDataParam("title") String title, 18 @FormDataParam("ownerId") String ownerId, 19 @FormDataParam("realm") @DefaultValue("LOAN_PRODUCT") Realm ownerRealm, 20 @FormDataParam("type") @DefaultValue("SIMPLE_RISK") SurveyType type, 21 @FormDataParam("questions") String questions, 22 @FormDataParam("timeOut") int timeOut) { 23 Gson gson = new Gson(); 24 Survey survey = gson.fromJson(questions,Survey.class); 25 26 RealmEntity owner = StringUtils.isBlank(ownerId) 27 ? RealmEntity.of(Realm.CLIENT, appBean.getClientCode()) 28 : RealmEntity.of(ownerRealm, ownerId); 29 RealmEntity creator = RealmEntity.of(Realm.EMPLOYEE, ContextUtils.getCurrentEmployee(req).getEmployeeId()); 30 31 survey.setTitle(title); 32 survey.setOwner(owner); 33 survey.setType(type); 34 survey.setStatus(SurveyStatus.CREATED); 35 survey.setCreator(creator); 36 survey.setTimeCreated(new Date()); 37 survey.setTimeout(timeOut); 38 surveyManagementService.createSurvey(appBean.getClientCode(), survey); 39 return redirect("/cms/survey"); 40 }
其中Gson gson = new Gson();Survey survey = gson.fromJson(questions,Survey.class);是将字符串questions转换为survey对象的quesionts字段。
Survey类中的问题属性定义为private List<Question> questions = new ArrayList<>();