举个简单的例子吧,比如我要做一个考试系统,有20个题,都是单选,我计算分数的时候,希望通过一个大循环一下子算出来,这样如果name相同就比较好处理。但是name相同了以后,所有20个题只能有一个选出来的了,而不是每个题都能选一个了。
解决方案 »
- 今天发现个问题,javascript传参,和java不同,请高手提醒.
- 一个JS 文件调用的问题,跪求帮助!
- ajax传值问题!在线等。。。。
- 如何用js判断是否点击?
- 字符串中间含空格的情况如何用正则表达式匹配
- getMinutes() 部起作用了。
- 做了倒计时功能,点击空格触发事件开始倒计时,为什么会延时下啊,请帮忙看看
- 关于使用标记属性的问题
- 大家觉得javascripts运行稳定吗?(进来讨论讨论)
- 关于ASP的问题。急!
- 请问在JAVASCRIPT中用什么方法可以得到此页面的所有链接
- 在一个页面中选定多个复选的条目后,点击确认然后这个页面自动关闭,选定的条目在另外一个页面中自动显示出来,这样一个效果如何实现。谢
:
隐藏两个checkbox
<script language="javascript">
function doClick(obj){
var tarObj;
if(obj.name=="answer_1"){
tarObj=document.getElementById("answerCheckbox_1");
tarObj.value = obj.value;
tarObj.checked=true;
return;
}
if(obj.name=="answer_1"){
tarObj=document.getElementById("answerCheckbox_2");
tarObj.value = obj.value;
tarObj.checked=true;
return;
}}
</script><html>
<body>
<form name='questionForm'>
<input type="checkbox" name=answer id="answerCheckbox_1" value="" style="display:none">
<input type="checkbox" name=answer id="answerCheckbox_2" value="" style="display:none"> <fieldset>
<input type="radio" name=answer_1 value=1 id=1 onclick="doClick(this)">
<input type="radio" name=answer_1 value=2 id=1 onclick="doClick(this)">
<input type="radio" name=answer_1 value=3 id=1 onclick="doClick(this)">
</fieldset>
<fieldset>
<input type="radio" name=answer_2 value=4 id=2 onclick="doClick(this)">
<input type="radio" name=answer_2 value=5 id=2 onclick="doClick(this)">
<input type="radio" name=answer_2 value=6 id=2 onclick="doClick(this)">
</fieldset>
<input type=button onclick=getRealValue()>
</form>
</body>
</html>
<body>
<form name='questionForm'>
<input type="radio" id=answer value=1 name=1>
<input type="radio" id=answer value=2 name=1>
<input type="radio" id=answer value=3 name=1>
<input type="radio" id=answer value=4 name=2>
<input type="radio" id=answer value=5 name=2>
<input type="radio" id=answer value=6 name=2>
<input type=button onclick=getRealValue()>
</form>
</body>
</html>
function doClick(obj){
var tarObj;
if(obj.name=="answer_1"){
tarObj=document.getElementById("answerCheckbox_1");
tarObj.value = obj.value;
tarObj.checked=true;
return;
}
if(obj.name=="answer_2"){
tarObj=document.getElementById("answerCheckbox_2");
tarObj.value = obj.value;
tarObj.checked=true;
return;
}}function getRealValue(){
tarObj=document.getElementsByName("answer");
for (var i=0;i<tarObj.length;i++){
alert(tarObj[i].value);
}
}
</script><html>
<body>
<form name='questionForm'>
<input type="checkbox" name="answer" id="answerCheckbox_1" value="" style="display:none">
<input type="checkbox" name="answer" id="answerCheckbox_2" value="" style="display:none"> <fieldset>
<input type="radio" name="answer_1" value=1 id=1 onclick="doClick(this)">
<input type="radio" name="answer_1" value=2 id=1 onclick="doClick(this)">
<input type="radio" name="answer_1" value=3 id=1 onclick="doClick(this)">
</fieldset>
<fieldset>
<input type="radio" name="answer_2" value=4 id=2 onclick="doClick(this)">
<input type="radio" name="answer_2" value=5 id=2 onclick="doClick(this)">
<input type="radio" name="answer_2" value=6 id=2 onclick="doClick(this)">
</fieldset>
<input type=button onclick="getRealValue()">
</form>
</body>
</html>
只有通过var obj = document.getElementById,obj才能获得type属性么?(即obj.type有定义)。而通过document.getElementsByName似乎不能。
只有通过document.getElementsByName才能获得length么?通过document.getElementById似乎不能。
这是我试验了一下的结果,不知道是不是有这个规律?
如果要获得value属性呢?等等,又没有比较好的资料?
这个问题搞定的话,三个帖子同时结,thx so much。
document.getElementById 取得的是一个对象所以getElementsByName才有长度,如果使用type,可以这样document.getElementsByName("name")[0].type
for(var i=0; i<arr.length; i++)
{
if(arr[i].checked)
alert(arr[i].value);
}