Project Practices:
There are a total of 25 questions for this exam, each of which is a two-choice and One-choice question with a total score of 100.
The javascript code is as follows:
1 /**//**
2 * @ author georgewing
3 */
4 function preparecheckbox (){
5 Document. getelementbyid ("Submit"). onclick = function (){
6 selectedcheckbox (4 );
7}
8}
9 function selectedcheckbox (x ){
10 var oinput = Document. getelementsbytagname ("input ");
11 var itotal = 0;
12 For (VAR I = 0; I <oinput. length; I ++ ){
13 if (oinput [I]. classname = "checkedradio "){
14 if (oinput [I]. Checked ){
15 // Add X point
16 itotal = itotal + X;
17}
18 else {
19 // Add 0 point
20 itotal = itotal + 0;
21}
22}
23
24}
25 document. getelementbyid ("Total"). setattribute ("value", itotal );
26 alert (itotal );
27}
The above Code has the problem of unnecessary DOM-2 programming model, the program is improved below:
var formElms = document.forms['form'].elements;var totalElm=formElms['Total'];var totalNum = 0;var selectedCheckBox = function(x){ for(var i=0;i< formElms.length;i++) {if(formElms[i].type=='radio') { if(formElms[i].checked) { //add x point totalNum = totalNum + x; } else { // add 0 point totalNum = totalNum + 0; } } } //End for-Loop totalElm.value = totalNum; alert(totalElm.value);};document.forms['form'].onsubmit = function() { selectedCheckBox(4);}