2016-12-12 31 views
-1

我是目前编程新手,处理传递值。我正在创建一个小问题来帮助我的编程技巧。我想从下拉列表中收集多个值,并将其打印出来显示给用户,如图所示。我不知道如何构建正确的结果的JavaScript。非常感谢帮助。传递多个值javascript(表单)

myTimer = setTimeout('alert("Time out")', 600000); /*alert will appear after 60 second*/ 
 

 
function processMyForm(frm) { 
 
    var chosenans1 = getRadioValue(frm.answer1); 
 
    var a1; 
 

 
    var chosenans2 = document.getElementById(frm.answer2).value; 
 
    var a2; 
 

 
    var chosenans3 = getRadioValue(frm.answer3); 
 
    var a3; 
 

 
    var chosenans4 = document.getElementById(frm.answer4).value; 
 
    var a4; 
 

 
    var chosenans5 = getRadioValue(frm.answer5); 
 
    var a5; 
 
    var chosenans6 = getRadioValue(frm.answer6); 
 
    var a6; 
 

 
    // ---------------------------- 
 

 
    var total; 
 

 
    // -------------------------------- 
 

 
    if (chosenans1 == "England") /*if radio value of answer1 is "England"*/ { 
 
    a1 = 2; /*correct answer for 2 marks*/ 
 
    document.getElementById("result1").innerHTML = ("<span class='correct'>Your answer to question 1 is correct.</span> "); /*print out correct at bottom of the page at <div id = "result1">*/ 
 
    } else { 
 
    a1 = -1; /*incorrect answer for -1 mark*/ 
 
    document.getElementById("result1").innerHTML = ("<span class='incorrect'>The correct answer to question 1 is 'England'.</span>"); /*print out incorrect at <div id = "result1">*/ 
 
    } 
 

 
    // --------------------- 
 

 
    if (chosenans2 == "Stonehenge") { 
 
    a2 = 2; 
 
    document.getElementById("result2").innerHTML = ("<span class='correct'>Your answer to question 2 is correct.</span>"); 
 
    } else { 
 
    a2 = -1; 
 
    document.getElementById("result2").innerHTML = ("<span class='incorrect'>The correct answer to question 2 is 'Stonehenge'.</span>"); 
 
    } 
 

 
    // ------------------- 
 

 
    if (chosenans3 == "2012") { 
 
    a3 = 2; 
 
    document.getElementById("result3").innerHTML = ("<span class='correct'>Your answer to question 3 is correct.</span>"); 
 
    } else { 
 
    a3 = -1; 
 
    document.getElementById("result3").innerHTML = ("<span class='incorrect'>The correct answer to question 3 is '2012'.</span>"); 
 
    } 
 

 
    // --------------------- 
 

 
    if (chosenans4 == "River Thames") { 
 
    a4 = 2; 
 
    document.getElementById("result4").innerHTML = ("<span class='correct'>Your answer to question 4 is correct.</span>"); 
 
    } else { 
 
    a4 = -1; 
 
    document.getElementById("result4").innerHTML = ("<span class='incorrect'>The correct answer to question 4 is 'River Thames'.</span>"); 
 
    } 
 

 
    // ------------------------- 
 

 
    if (chosenans5 == "Pound") { 
 
    a5 = 2; 
 
    document.getElementById("result5").innerHTML = ("<span class='correct'>Your answer to question 5 is correct.</span>"); 
 
    } else { 
 
    a5 = -1; 
 
    document.getElementById("result5").innerHTML = ("<span class='incorrect'>The correct answer to question 5 is 'Pound'.</span>"); 
 
    } 
 

 
    if (chosenans6 == "David Cameron") { 
 
    a6 = 2; 
 
    document.getElementById("result6").innerHTML = ("<span class='correct'>Your answer to question 6 is correct.</span>"); 
 
    } else { 
 
    a6 = -1; 
 
    document.getElementById("result6").innerHTML = ("<span class='incorrect'>The correct answer to question 6 is 'David Cameron'.</span>"); 
 
    } 
 

 
    // -------------------------------------------------------- 
 

 
    total = a1 + a2 + a3 + a4 + a5 + a6; /*add marks(2 or -1) together*/ 
 

 
    document.getElementById("result").innerHTML = ("Your mark is " + total); //print out your total mark at <div id = "result"> 
 
    alert("Your mark is " + total); //prompt total mark in small window 
 

 
    if (total < 8) { 
 
    document.body.style.backgroundImage = "none"; //remove background image 
 
    document.body.style.backgroundColor = "#bb0000"; //add a background colour 
 
    } else { 
 
    document.body.style.backgroundImage = "none"; 
 
    document.body.style.backgroundColor = "#006600"; 
 
    } 
 

 
    clearTimeout(myTimer); //stop timer 
 
} 
 

 

 
function getValue(qArray) { //get value from radio array 
 
    var i; 
 
    for (i = 0; i < qArray.length; i++) { 
 
     if (qArray[i].checked) return qArray[i].value; 
 
    } 
 
    return ""; 
 
    }
<input type="radio" name="answer1" value="England">England<br/> 
 
<input type="radio" name="answer1" value="Scotland">Scotland<br/> 
 
<input type="radio" name="answer1" value="Wales">Wales<br/> 
 
<input type="radio" name="answer1" value="Northern Ireland">Northern Ireland<br/> 
 
<!--radio buttons with different values--> 
 

 
<br/>2: Which of the following is not in London:<br/> 
 
<select id="answer 2"> 
 
    <option value="St Paul's Cathedral">St Paul's Cathedral</option> 
 
    <option value="Buckingham Palace">Buckingham Palace</option> 
 
    <option value="Stonehenge">Stonehenge</option> 
 
</select> 
 

 
<br/>3: Which year's Olympic games was hosted by London:<br/> 
 
<input type="radio" name="answer3" value="2012">2012<br/> 
 
<input type="radio" name="answer3" value="2008">2008<br/> 
 
<input type="radio" name="answer3" value="2004">2004<br/> 
 

 
<br/>4: Which river runs through London:<br/> 
 
<input type="checkbox" name="answer4" value="La Seine">La Seine<br> 
 
<input type="checkbox" name="answer4" value="Rhine River">Rhine River<br> 
 
<input type="checkbox" name="answer4" value="River Thames">River Thames<br><br><br> 
 

 
<input type="button" onclick="processMyForm(this.form)" value="Check answers"> 
 
<!--a button called "Check answers" will run processMyForm procedure when user click--> 
 

 
<br/><br/> 
 
<div id="result"></div> 
 
<!--print out user's total mark--> 
 
<br/> 
 
<div id="result1"></div> 
 
<div id="result2"></div> 
 
<!--print out correct or incorrect for each question--> 
 
<br/> 
 
<br/>

+0

的[获取使用Javascript的单选按钮值](http://stackoverflow.com/questions/9618504/get-radio-button-value-with-javascript) – Illdapt

+0

这可能重复是不同的 –

回答

0

老弟,你有很多的错误,因为(没有定义在你的HTML标记的标签的形式,试图达到不同的名称元素,缺码的(你没有声明HTML答案5和6,但你试图得到回应这些元素(永远不会工作),

尝试按照代码波纹管,现在正在工作。

希望它可以帮助你...

myTimer = setTimeout('alert("Time out")', 600000); /*alert will appear after 60 second*/ 
 

 
function processMyForm() { 
 
    var chosenans1 = document.mainForm.answer1.value; 
 
    var a1; 
 

 
    //alert(document.mainForm.answer1.value); 
 
    //alert(document.getElementById('answer2')); 
 
    var chosenans2 = document.getElementById('answer2').value; 
 
    var a2; 
 

 
    var chosenans3 = document.mainForm.answer3.value; 
 
    var a3; 
 
    //alert(document.mainForm.answer3.value); 
 

 
    var chosenans4 = document.mainForm.answer4.checked; 
 
    var a4; 
 

 
    //var chosenans5 = getRadioValue(frm.answer5); 
 
    //var a5; 
 
    //var chosenans6 = getRadioValue(frm.answer6); 
 
    //var a6; 
 

 
    // ---------------------------- 
 

 
    var total; 
 

 
    // -------------------------------- 
 

 
    if (chosenans1 == "England") /*if radio value of answer1 is "England"*/ { 
 
    a1 = 2; /*correct answer for 2 marks*/ 
 
    document.getElementById("result1").innerHTML = "<span class='correct'>Your answer to question 1 is correct.</span> "; /*print out correct at bottom of the page at <div id = "result1">*/ 
 
    } else { 
 
    a1 = -1; /*incorrect answer for -1 mark*/ 
 
    document.getElementById("result1").innerHTML = "<span class='incorrect'>The correct answer to question 1 is 'England'.</span>"; /*print out incorrect at <div id = "result1">*/ 
 
    } 
 

 
    // --------------------- 
 

 
    if (chosenans2 == "Stonehenge") { 
 
    a2 = 2; 
 
    document.getElementById("result2").innerHTML = "<span class='correct'>Your answer to question 2 is correct.</span>"; 
 
    } else { 
 
    a2 = -1; 
 
    document.getElementById("result2").innerHTML = "<span class='incorrect'>The correct answer to question 2 is 'Stonehenge'.</span>"; 
 
    } 
 

 
    // ------------------- 
 

 
    if (chosenans3 == "2012") { 
 
    a3 = 2; 
 
    document.getElementById("result3").innerHTML = "<span class='correct'>Your answer to question 3 is correct.</span>"; 
 
    } else { 
 
    a3 = -1; 
 
    document.getElementById("result3").innerHTML = "<span class='incorrect'>The correct answer to question 3 is '2012'.</span>"; 
 
    } 
 

 
    // --------------------- 
 

 
    if (chosenans4 == "River Thames") { 
 
    a4 = 2; 
 
    document.getElementById("result4").innerHTML = "<span class='correct'>Your answer to question 4 is correct.</span>"; 
 
    } else { 
 
    a4 = -1; 
 
    document.getElementById("result4").innerHTML = "<span class='incorrect'>The correct answer to question 4 is 'River Thames'.</span>"; 
 
    } 
 

 
    // ------------------------- 
 

 
    /*if (chosenans5 == "Pound") { 
 
    a5 = 2; 
 
    document.getElementById("result5").innerHTML = ("<span class='correct'>Your answer to question 5 is correct.</span>"); 
 
    } else { 
 
    a5 = -1; 
 
    document.getElementById("result5").innerHTML = ("<span class='incorrect'>The correct answer to question 5 is 'Pound'.</span>"); 
 
    } 
 

 
    if (chosenans6 == "David Cameron") { 
 
    a6 = 2; 
 
    document.getElementById("result6").innerHTML = ("<span class='correct'>Your answer to question 6 is correct.</span>"); 
 
    } else { 
 
    a6 = -1; 
 
    document.getElementById("result6").innerHTML = ("<span class='incorrect'>The correct answer to question 6 is 'David Cameron'.</span>"); 
 
    }*/ 
 

 
    // -------------------------------------------------------- 
 

 
    total = a1 + a2 + a3 + a4 ;//+ a5 + a6; /*add marks(2 or -1) together*/ 
 

 
    document.getElementById("result").innerHTML = ("Your mark is " + total); //print out your total mark at <div id = "result"> 
 
    alert("Your mark is " + total); //prompt total mark in small window 
 

 
    if (total < 4) { 
 
    document.body.style.backgroundImage = "none"; //remove background image 
 
    document.body.style.backgroundColor = "#bb0000"; //add a background colour 
 
    } else { 
 
    document.body.style.backgroundImage = "none"; 
 
    document.body.style.backgroundColor = "#006600"; 
 
    } 
 

 
    clearTimeout(myTimer); //stop timer 
 
} 
 

 

 
function getValue(qArray) { //get value from radio array 
 
    var i; 
 
    for (i = 0; i < qArray.length; i++) { 
 
     if (qArray[i].checked) return qArray[i].value; 
 
    } 
 
    return ""; 
 
    }
<form id="mainForm" name="mainForm"> 
 
<input type="radio" name="answer1" value="England">England<br/> 
 
<input type="radio" name="answer1" value="Scotland">Scotland<br/> 
 
<input type="radio" name="answer1" value="Wales">Wales<br/> 
 
<input type="radio" name="answer1" value="Northern Ireland">Northern Ireland<br/> 
 
<!--radio buttons with different values--> 
 

 
<br/>2: Which of the following is not in London:<br/> 
 
<select id="answer2"> 
 
    <option value="St Paul's Cathedral">St Paul's Cathedral</option> 
 
    <option value="Buckingham Palace">Buckingham Palace</option> 
 
    <option value="Stonehenge">Stonehenge</option> 
 
</select> 
 

 
<br/>3: Which year's Olympic games was hosted by London:<br/> 
 
<input type="radio" name="answer3" value="2012">2012<br/> 
 
<input type="radio" name="answer3" value="2008">2008<br/> 
 
<input type="radio" name="answer3" value="2004">2004<br/> 
 

 
<br/>4: Which river runs through London:<br/> 
 
<input type="checkbox" name="answer4" value="La Seine">La Seine<br> 
 
<input type="checkbox" name="answer4" value="Rhine River">Rhine River<br> 
 
<input type="checkbox" name="answer4" value="River Thames">River Thames<br><br><br> 
 
    
 
<input type="button" onclick="processMyForm()" value="Check answers"> 
 
<!--a button called "Check answers" will run processMyForm procedure when user click--> 
 

 
</form> 
 

 
<br/><br/> 
 
<div id="result"></div> 
 
<!--print out user's total mark--> 
 
<br/> 
 
<div id="result1"></div> 
 
<div id="result2"></div> 
 
<div id="result3"></div> 
 
<div id="result4"></div> 
 
<!--print out correct or incorrect for each question--> 
 
<br/> 
 
<br/>