求一段JS选择题的代码

JavaScript06

求一段JS选择题的代码,第1张

<!DOCTYPE html>

<html>

<head>

<title>test</title>

</head>

<body>

<p>选择题1:</p>

<form>

<input type="radio" name="score" value="1">1:一分<br>

<input type="radio" name="score" value="2">2:二分<br>

<input type="radio" name="score" value="3">3:三分<br>

<input type="radio" name="score" value="4">4:四分<br>

<input type="radio" name="score" value="5">5:五分<br>

</form>

<p>选择题2:</p>

<form>

<input type="radio" name="score" value="1">1:一分<br>

<input type="radio" name="score" value="2">2:二分<br>

<input type="radio" name="score" value="3">3:三分<br>

<input type="radio" name="score" value="4">4:四分<br>

<input type="radio" name="score" value="5">5:五分<br>

</form>

<p>选择题3:</p>

<form>

<input type="radio" name="score" value="1">1:一分<br>

<input type="radio" name="score" value="2">2:二分<br>

<input type="radio" name="score" value="3">3:三分<br>

<input type="radio" name="score" value="4">4:四分<br>

<input type="radio" name="score" value="5">5:五分<br>

</form>

<p>选择题4:</p>

<form>

<input type="radio" name="score" value="1">1:一分<br>

<input type="radio" name="score" value="2">2:二分<br>

<input type="radio" name="score" value="3">3:三分<br>

<input type="radio" name="score" value="4">4:四分<br>

<input type="radio" name="score" value="5">5:五分<br>

</form>

<p>你想添加多少题都行,为版幅起见我只列出四题。。。。。</p>

<button id="button">得分是</button>

<script type="text/javascript">

var sco=document.getElementsByTagName("input")

var butn=document.getElementById("button")

butn.onclick=function(){

var gec=0

for(var i=0i<sco.lengthi++){

if(sco[i].checked == true){

var sc=parseInt(sco[i].value)

gec+=sc

}

}

alert(gec)

}

</script>

</body>

</html

>

这问题太麻烦,我完成了你说的功能,里面的格式和布局自己调吧。 <HTML><HEAD><TITLE>js编辑选择题</TITLE><SCRIPT language="JavaScript">function show() { var str = ""str = str + panduan("wenti1", 1, "第一题回答")str = str + panduan("wenti2", 0, "第二题回答")alert(str)} function panduan(name, daan, xinxi) { var jieguo = ""var jieguo1 = document.getElementsByName(name)if (jieguo1[daan].checked == true) { jieguo = jieguo + xinxi + "正确。\n"} else { jieguo = jieguo + xinxi + "错误。\n"} return jieguo} </SCRIPT></HEAD><BODY> <FORM name="myForm">第1题 <br>《越狱》的男主角叫什么名字?<br><input type="radio" name="wenti1" />A Wentworth Miller <BR><input type="radio" name="wenti1" />B Michael Scofield <BR><input type="radio" name="wenti1" />C Lincoln Burrows <BR><input type="radio" name="wenti1" />D Sara Tancredi <BR><BR><BR>第2题 <br>《越狱》女主角是谁的女儿?<br><input type="radio" name="wenti2" />A Tancredi州长 <BR><input type="radio" name="wenti2" />B D.B.Cooper <BR><input type="radio" name="wenti2" />C 监狱长Pope <BR><input type="radio" name="wenti2" />D 剧情没有介绍 <BR><INPUT TYPE="button" onclick="show()" value="提交"> </FORM></BODY></HTML>

你说的多项选择题,我是不是可以理解成多选??

多选的实现是这样的:

第一: 必须将多选框放到form里面。

第二: 然后name属性完全一样,value不相同。这样当你提交到Action中的时候,只需要使用request对象获取toselect的值就行了。

第三: 获取值:request.getParameterValues("toselect"),就会将选中的多选框里面的value获取,并且返回一个String[]数组,这个数组里面就有你想要的值:即选中的值

<html>

<body>

<form>

<input type = "checkbox" value = "A" name = "toselect"/>A

<input type = "checkbox" value = "B" name = "toselect"/>B

<input type = "checkbox" value = "C" name = "toselect"/>C

<input type = "checkbox" value = "D" name = "toselect"/>D

</form>

</body>

</html>