项目实战中遇到的:
该考试题目共有25道,每一道都是2选1的选择题,总分是100分。
JavaScript代码如下:
1
/**/
/**
2
* @author georgewing
3
*/
4![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
function
prepareCheckBox()
{
5![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
document.getElementById("submit").onclick = function()
{
6
selectedCheckBox(4);
7
}
8
}
9![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
function
selectedCheckBox(x)
{
10
var oInput = document.getElementsByTagName("input");
11
var iTotal = 0;
12![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(var i=0;i<oInput.length;i++)
{
13![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(oInput[i].className == "checkedRadio")
{
14![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(oInput[i].checked)
{
15
//add x point
16
iTotal = iTotal + x;
17
}
18![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
19
// add 0 point
20
iTotal = iTotal + 0;
21
}
22
}
23
24
}
25
document.getElementById("Total").setAttribute("value", iTotal);
26
alert(iTotal);
27
}
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
2
![](/Images/OutliningIndicators/InBlock.gif)
3
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
5
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
8
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
9
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
13
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
14
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
上面的代码存在不必要的DOM-2编程模型的问题,下面进行了程序的改进:
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); }