answer_net.js 3.19 KB

//14
var paperType=4;
function submitAnswer(){
    var result={};
    
    var usreName=$("#userName").val();
    var userPhone=$("#userPhone").val();
    if(userName==null||userName==undefined||userName==''){
        layer.msg("请填写姓名")
        return;
    }
    if(userPhone==null||userPhone==undefined||userPhone==''){
         //倒计时时间到了,还没有填写手机号信息,则无效
        if(maxtime==0){
            layer.msg('时间到!');
        }else{
            layer.msg("请填写电话号")
        }  
        return; 
    }
    if(!checkPhone()){
        return;
    }
    var paramAnswer={
        "paperType":paperType,
        "username":usreName,
        "userPhone":userPhone,
        "examTime":examTime
    }
    result.answer=paramAnswer;

    var d=[];
    var orderNum=0;
    $("textarea[name='answer']").each(function(j,item){
        var param={
            "order":j+1,
            "answer":item.value
        }
        orderNum=j;
        d[j]=param;
      });
    $("input[name='answer']").each(function(j,item){
        var num=j+1+orderNum;
      var param={
          "order":j+1+orderNum,
          "answer":item.value
      }
      d[num]=param;
    });
    
    result.answerDetails=d;
    console.log(result)
    
   
    $.ajax({
        type: "post",
        dataType: "json",
        contentType:"application/json",
        url:prefix+"/answer/submitExam",
        data: JSON.stringify( result),
        success:function(r) {
            if (r.code == 200) {
                layer.open({
                    time:3000,
                    content:'提交成功!每人限制提交一次。'
                })
                getAnswerByPhone(userPhone);
                permitKey()
            } else if(r.code==202){
                layer.open({
                    time:3000,
                    content:r.msg
                })
                getAnswerByPhone(userPhone);
                permitKey()
            }
        }
    });

}

function getAnswerByPhone(userPhone){
    //var userPhone=$("#userPhone").val();
    var result={
            "answer":{
            "userPhone":userPhone,
            "paperType":paperType
            },
             "answerDetails":[{
             }]
        }
    $.ajax({
        type: "post",
        dataType: "json",
        contentType:"application/json",
        url:prefix+"/answer/getAnswerByPhone",
        data: JSON.stringify( result),
        success:function(r) {
            if (r.code == 200) {
                $("#hiddenSubmit").hide();
                $(".pull-right").hide();
                var answer=r.data.answer;
                var answerDetails=r.data.answerDetails;
                $("#userPhone").val(answer.userPhone);
                $("#userName").val(answer.username);
                $("#userPhone").attr("readOnly",true);
                $("#userName").attr("readOnly",true);
                answerDetails.forEach(function(obj,index,array){
                    $("textarea[name='answer']")[index].value=obj.answer;
                    $("textarea[name='answer']")[index].readOnly=true;
                });
            } else {
            	
            }
        }
    });
}

$(document).ready(function(){
    
    });