JavaScript programming solves the exam score statistics Problem

Source: Internet
Author: User

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);}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.