以获得的分数重定向的表单

时间:2014-02-02 16:43:23

标签: javascript html

我有一个html表单有16个问题,单选按钮回答“是或否”,每个都有不同的值,在q16后用户点击下一个并取决于用户被带到右页的分数!共3页

以下是我所做的代码工作到q9,但看不到我出错的地方

function submitForm(){
    var totalScore = 0;

    if(document.myform.username.value.length == 0){ //make sure a name has been entered
        alert('Please enter a name.');
    }else if(document.myform.q1[0].checked == false && document.myform.q1[1].checked == false){// make sure q1 has been answered
        alert('Please answer question 1.');
    }else if(document.myform.q2[0].checked == false && document.myform.q2[1].checked == false){// make sure q2 has been answered
        alert('Please answer question 2.');
    }else if(document.myform.q3[0].checked == false && document.myform.q3[1].checked == false){// make sure q3 has been answered
        alert('Please answer question 3.');
    }else if(document.myform.q4[0].checked == false && document.myform.q4[1].checked == false){// make sure q4 has been answered
        alert('Please answer question 4.');
    }else if(document.myform.q5[0].checked == false && document.myform.q5[1].checked == false){// make sure q5 has been answered
        alert('Please answer question 5.');
    }else if(document.myform.q6[0].checked == false && document.myform.q6[1].checked == false){// make sure q6 has been answered
        alert('Please answer question 6.');
    }else if(document.myform.q7[0].checked == false && document.myform.q7[1].checked == false){// make sure q7 has been answered
        alert('Please answer question 7.');
    }else if(document.myform.q8[0].checked == false && document.myform.q8[1].checked == false){// make sure q8 has been answered
        alert('Please answer question 8.');
    }else if(document.myform.q9[0].checked == false && document.myform.q9[1].checked == false && document.myform.q9[2].checked == false){// make sure q9 has been answered
        alert('Please answer question 9.');
    }else if(document.myform.q10[0].checked == false && document.myform.q10[1].checked == false && document.myform.q10[2].checked == false){// make sure q10 has been answered
        alert('Please answer question 10.');
    }else if(document.myform.q11[0].checked == false && document.myform.q11[1].checked == false){// make sure q11 has been answered
        alert('Please answer question 11.');
    }else if(document.myform.q12[0].checked == false && document.myform.q12[1].checked == false){// make sure q12 has been answered
        alert('Please answer question 12.');
    }else if(document.myform.q13[0].checked == false && document.myform.q13[1].checked == false){// make sure q13 has been answered
        alert('Please answer question 13.');
    }else if(document.myform.q14[0].checked == false && document.myform.q14[1].checked == false){// make sure q14 has been answered
        alert('Please answer question 14.');
    }else if(document.myform.q15[0].checked == false && document.myform.q15[1].checked == false && document.myform.q15[2].checked == false){// make sure q15 has been answered
        alert('Please answer question 15.');
    }else if(document.myform.q16[0].checked == false && document.myform.q16[1].checked == false && document.myform.q16[2].checked == false){// make sure q16 has been answered
        alert('Please answer question 16.');
    }else{ //everything has been entered

        var q1Score = 0 //work out the value of q1
        if(document.myform.q1[0].checked == true){
            q1Score=document.myform.q1[0].value;
        }else if(document.myform.q1[1].checked == true){
            q1Score=document.myform.q1[1].value;
        }

        var q2Score = 0 //work out the value of q2
        if(document.myform.q2[0].checked == true){
            q2Score=document.myform.q2[0].value;
        }else if(document.myform.q2[1].checked == true){
            q2Score=document.myform.q2[1].value;
        }

        var q3Score = 0 //work out the value of q3
        if(document.myform.q3[0].checked == true){
            q3Score=document.myform.q3[0].value;
        }else if(document.myform.q3[1].checked == true){
            q3Score=document.myform.q3[1].value;
        }

        var q4Score = 0 //work out the value of q4
        if(document.myform.q4[0].checked == true){
            q4Score=document.myform.q4[0].value;
        }else if(document.myform.q4[1].checked == true){
            q4Score=document.myform.q4[1].value;
        }

         var q5Score = 0 //work out the value of q5
        if(document.myform.q5[0].checked == true){
            q5Score=document.myform.q5[0].value;
        }else if(document.myform.q5[1].checked == true){
            q5Score=document.myform.q5[1].value;
        }
         var q6Score = 0 //work out the value of q6
        if(document.myform.q6[0].checked == true){
            q6Score=document.myform.q6[0].value;
        }else if(document.myform.q6[1].checked == true){
            q6Score=document.myform.q6[1].value;
        }
         var q7Score = 0 //work out the value of q7
        if(document.myform.q7[0].checked == true){
            q7Score=document.myform.q7[0].value;
        }else if(document.myform.q7[1].checked == true){
            q7Score=document.myform.q7[1].value;
        }
         var q8Score = 0 //work out the value of q8
        if(document.myform.q8[0].checked == true){
            q8Score=document.myform.q8[0].value;
        }else if(document.myform.q8[1].checked == true){
            q8Score=document.myform.q8[1].value;
        }
         var q9Score = 0 //work out the value of q9
        if(document.myform.q9[0].checked == true){
            q9Score=document.myform.q9[0].value;
        }else if(document.myform.q9[1].checked == true){
            q9Score=document.myform.q9[1].value;
        }else if(document.myform.q9[2].checked == true){
            q9Score=document.myform.q9[2].value;    
        }
         var q10Score = 0 //work out the value of q10
        if(document.myform.q10[0].checked == true){
            q10Score=document.myform.q10[0].value;
        }else if(document.myform.q10[1].checked == true){
            q10Score=document.myform.q10[1].value;
        }else if(document.myform.q10[2].checked == true){
            q10Score=document.myform.q10[2].value;  
        }
         var q11Score = 0 //work out the value of q11
        if(document.myform.q11[0].checked == true){
            q11Score=document.myform.q11[0].value;
        }else if(document.myform.q11[1].checked == true){
            q11Score=document.myform.q11[1].value;
        }
         var q12Score = 0 //work out the value of q12
        if(document.myform.q12[0].checked == true){
            q12Score=document.myform.q12[0].value;
        }else if(document.myform.q12[1].checked == true){
            q12Score=document.myform.q12[1].value;
        }
         var q13Score = 0 //work out the value of q13
        if(document.myform.q13[0].checked == true){
            q13Score=document.myform.q13[0].value;
        }else if(document.myform.q13[1].checked == true){
            q13Score=document.myform.q13[1].value;
        }
         var q14Score = 0 //work out the value of q14
        if(document.myform.q14[0].checked == true){
            q14Score=document.myform.q14[0].value;
        }else if(document.myform.q14[1].checked == true){
            q14Score=document.myform.q14[1].value;
        }
         var q15Score = 0 //work out the value of q15
        if(document.myform.q15[0].checked == true){
            q15Score=document.myform.q15[0].value;
        }else if(document.myform.q15[1].checked == true){
            q15Score=document.myform.q15[1].value;
        }else if(document.myform.q15[2].checked == true){
            q15Score=document.myform.q15[2].value;  
        }
        var q16Score = 0 //work out the value of q16
        if(document.myform.q16[0].checked == true){
            q16Score=document.myform.q16[0].value;
        }else if(document.myform.q16[1].checked == true){
            q16Score=document.myform.q16[1].value;
        }else if(document.myform.q16[2].checked == true){
            q16Score=document.myform.q16[2].value;  
        }
        //add the scores together
        totalScore=parseInt(q1Score)+parseInt(q2Score)+parseInt(q3Score)+parseInt(q4Score)+parseInt(q5Score)+parseInt(q6Score)+parseInt(q7Score)+parseInt(q8Score)+parseInt(q9Score)+parseInt(q10Score)+parseInt(q11Score)+parseInt(q12Scorce)+parseInt(q13Score)+parseInt(q14Score)+parseInt(q15Score)+parseInt(q16Score);

        if(totalScore<=15){ //if it's less than or equal to 15 go to this page...
            window.location.href='greenzonePage.html?name='+document.myform.username.value+'&score='+totalScore;
        }else if (totalScore >=16 && totalScore <30){ //go to this page
            window.location.href='yellowzonePage.html?name='+document.myform.username.value+'&score='+totalScore;
        }else {
            window.location.href='redzonePage.html?name='+document.myform.username.value+'&score='+totalScore;
        }

    }

}

1 个答案:

答案 0 :(得分:0)

为了避免这种重复的代码,你可能想要像Bergi在他的评论中指出的那样使用循环。你可以做这样的事情,例如:(注意 - 未经测试!)

function submitForm() {
    if(document.myform.username.value.length == 0) { //make sure a name has been entered
        alert('Please enter a name.');
        return;
    }

    var totalScore = 0;

    var threeAnswerQuestions = [9, 10, 15, 16];
    var numberOfQuestions = 16;

    for(var i = 1; i <= numberOfQuestions; i++) {
        var numerOfAnswers = threeAnswerQuestions.indexOf(i) != -1 ? 3 : 2;
        var answerChecked = false;

        for(var j = 0; j < numerOfAnswers; j++) {
            if(document.myform['q'+i][j].checked) {
                answerChecked = true;
                totalScore += parseInt(document.myform['q'+i][j].value);
                break;
            }
        }
        if(!answerChecked) {
            alert('Please answer question ' + i + '.');
            return;
        }
    }

    if(totalScore <= 15) { //if it's less than or equal to 15 go to this page...
        window.location.href='greenzonePage.html?name='+document.myform.username.value+'&score='+totalScore;
    } else if (totalScore >= 16 && totalScore < 30) { //go to this page
        window.location.href='yellowzonePage.html?name='+document.myform.username.value+'&score='+totalScore;
    } else {
        window.location.href='redzonePage.html?name='+document.myform.username.value+'&score='+totalScore;
    }
}