-----------------------------------------------------------------------------------------
<input type="radio" name="myRadio" value="1">
<input type="radio" name="myRadio" value="2">
...
当有很多myRadio的时候,如何直接得到所选radio的值,不用循环。
---------------------------------------------------------------
将radio放在span或div内,检查它的onclick事件
<span onclick="getRadio()">
<input type="radio" name="myRadio" value="1">1
<input type="radio" name="myRadio" value="2">2
<input type="radio" name="myRadio" value="3">3
<input type="txet" name="sel">
</span>
<script>
function getRadio(){
e = event.srcElement;
if(e.type=="radio" && e.name=="myRadio")
sel.value = e.value
}
</script>
---------------------------------------------------------------
<input type=radio name=r1 value="a">a<br>
<input type=radio name=r1 value="b">b<br>
<input type=radio name=r1 value="c">c<br>
<input type=radio name=r1 value="d">d<br>
<input type=radio name=r1 value="e">e<br>
<input type=button value=ok onclick=cc()>
<script language=javascript>
function cc()
{
var aa = document.getElementsByName("r1");
for (var i=0; i<aa.length; i++)
{
if(aa[i].checked)alert(aa[i].value);
}
}
</script>
上面的是用循环的做法,不用循环的方法是:
<input type="radio" name="myRadio" value="1" onclick="Javascript:ss=this.value">
<input type="radio" name="myRadio" value="2" onclick="Javascript:ss=this.value">
你用JS定义一个全局变量SS,用这种方法,到最后只要判断变量不为空即可。
本文介绍了一种不使用循环来直接获取选中Radio按钮值的方法。通过为每个Radio按钮添加onclick事件,可以立即将其值赋给一个全局变量,从而简化了传统循环查找的方式。
252

被折叠的 条评论
为什么被折叠?



