answer_dq_B.js 4.35 KB
//24input,7个textarea
var paperType=6
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;
    
    $("input[name='answer']").each(function(j,item){
        var param={
            "orders":j+1,
            "answer":item.value
        }
        orderNum=j;
        d[j]=param;
    });
    var pic='';
    $("input[name='pic']").each(function(){
        pic+=$(this).val()+","
    })
    var pic2='';
    $("input[name='pic2']").each(function(){
        pic2+=$(this).val()+","
    })
    var pic3='';
    $("input[name='pic3']").each(function(){
        pic3+=$(this).val()+","
    })
    $("textarea[name='answer']").each(function(j,item){
        var num=j+1+orderNum;
        var param={};
        if(j==0){
            param={
                "orders":num,
                "answer":item.value,
                "pic":pic
            }
        }
        if(j==1){
            param={
                "orders":num,
                "answer":item.value,
                "pic":pic2
            }
        }
        if(j==2){
            param={
                "orders":num,
                "answer":item.value,
                "pic":pic3
            }
        }
        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) {
                //location.href =  '#';
                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);
                $("#paperType").val(answer.paperTypeName);
                $("#userPhone").attr("readOnly",true);
                $("#userName").attr("readOnly",true);
                $("#paperType").attr("readOnly",true);
                var inputIndex=0;
                answerDetails.forEach(function(obj,index,array){
                    if(index<=15){
                        $("input[name='answer']")[index].value=obj.answer;
                        $("input[name='answer']")[index].readOnly=true;
                    }else{
                        $("textarea[name='answer']")[inputIndex].value=obj.answer;
                        $("textarea[name='answer']")[inputIndex].readOnly=true;
                        inputIndex++;
                    } 
                });
            } else {
            	
            }
        }
    });
}

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