JS 操作 radio input(cc问卷管理)

时间:2022-09-20 17:41:23

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部分代码

                 $('.handleSaveBtn').click(function(){
//判空,标题、有效期、题干、选项、分值
if ($("#title1").val() === '') {
alert("问卷标题不可为空");
return;
}
if ($("#timeOut1").val() === '') {
alert("问卷有效期不可为空");
return;
}
var flag=0;
$("input[name^='q']").each(function(){
if($(this).val()===""){
alert("题干、选项、分值等不可为空!");
flag=1;
return false;
}
});
if(flag===1){
return;
}
//组成问卷questions部分
var obj=new Object();
var questions=new Array();
var totalScore=0,score1,score2,score3,score4;
for(var i=0;i<10;i++){
var j=i+1;
var question=new Object();
var type=$("#type"+j).val();
question.type=type;
question.ordinal=j;
question.content=$("input[name='q"+j+"']").val();
if(type==='SINGLE'){
var choice1={"value":"A","content":$('input[name="q'+j+'c1"]').val(),"score":$('input[name="q'+j+'cm1"]').val()};
var choice2={"value":"B","content":$('input[name="q'+j+'c2"]').val(),"score":$('input[name="q'+j+'cm2"]').val()};
var choice3={"value":"C","content":$('input[name="q'+j+'c3"]').val(),"score":$('input[name="q'+j+'cm3"]').val()};
var choice4={"value":"D","content":$('input[name="q'+j+'c4"]').val(),"score":$('input[name="q'+j+'cm4"]').val()};
question.choices=[choice1,choice2,choice3,choice4]; score1=$('input[name="q'+j+'cm1"]').val();
score2=$('input[name="q'+j+'cm2"]').val();
score3=$('input[name="q'+j+'cm3"]').val();
score4=$('input[name="q'+j+'cm4"]').val();
if(score1<score2){
score1=score2;
}
if(score3<score4){
score1=score2;
}
if(score1<score3){
score1=score3;
}
}else if(type==='MULTIPLE'){
var choice1={"value":"A","content":"是","score":$('input[name="q'+j+'cm1"]').val()};
var choice2={"value":"B","content":"否","score":$('input[name="q'+j+'cm2"]').val()};
question.choices=[choice1,choice2]; score1=$('input[name="q'+j+'cm1"]').val();
score2=$('input[name="q'+j+'cm2"]').val();
if(score1<score2){
score1=score2;
}
}else{
var choice1={"value":"A","content":$('input[name="q'+j+'c1"]').val(),"score":$('input[name="q'+j+'cm1"]').val()};
question.choices=[choice1];
score1=$('input[name="q'+j+'cm1"]').val();
}
totalScore+score1;
questions.push(question);
}
if(totalScore>100){
alert("总分不能超过100!");
return;
}
obj.questions=questions;
var json=JSON.stringify(obj); var formData = new FormData();
formData.append("title",$("#title1").val());
formData.append("status",$("#status1").val());
formData.append("timeOut",$("#timeOut1").val());
formData.append("questions",json);
alert(json); //提交
var url = 'cms/survey/add2';
$.ajax({
url: url,
data: formData,
processData: false,
contentType: false,
type: 'POST',
success: function (job) {
console.log(job);
if (job) {
alert("问卷添加成功!");
location.reload();
} else {
alert('问卷添加失败');
}
}
}).fail(function () {
alert('问卷添加失败');
}); });
</script>
 其中第76行代码var json=JSON.stringify(obj);是将js对象转换为JSON对象。

java部分代码

     /**
* 手动添加问卷
*
* @param req
* @param title
* @param ownerId
* @param ownerRealm
* @param type
* @param questions
* @param timeOut
* @return
*/
@POST
@Path("/add2")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response addSurvey2(@Context HttpServletRequest req,
@FormDataParam("title") String title,
@FormDataParam("ownerId") String ownerId,
@FormDataParam("realm") @DefaultValue("LOAN_PRODUCT") Realm ownerRealm,
@FormDataParam("type") @DefaultValue("SIMPLE_RISK") SurveyType type,
@FormDataParam("questions") String questions,
@FormDataParam("timeOut") int timeOut) {
Gson gson = new Gson();
Survey survey = gson.fromJson(questions,Survey.class); RealmEntity owner = StringUtils.isBlank(ownerId)
? RealmEntity.of(Realm.CLIENT, appBean.getClientCode())
: RealmEntity.of(ownerRealm, ownerId);
RealmEntity creator = RealmEntity.of(Realm.EMPLOYEE, ContextUtils.getCurrentEmployee(req).getEmployeeId()); survey.setTitle(title);
survey.setOwner(owner);
survey.setType(type);
survey.setStatus(SurveyStatus.CREATED);
survey.setCreator(creator);
survey.setTimeCreated(new Date());
survey.setTimeout(timeOut);
surveyManagementService.createSurvey(appBean.getClientCode(), survey);
return redirect("/cms/survey");
}
其中Gson gson = new Gson();Survey survey = gson.fromJson(questions,Survey.class);是将字符串questions转换为survey对象的quesionts字段。
Survey类中的问题属性定义为private List<Question> questions = new ArrayList<>();