方法1.
<input type="radio" name="myRadio" value="1">
<input type="radio" name="myRadio" value="2">
...
当有很多myRadio的时候,如何直接得到所选radio的值,不用循环。
---------------------------------------------------------------
将radio放在span或div内,检查它的onclick事件
<span οnclick="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>
方法2.
<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 οnclick=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">
<input type="radio" name="myRadio" value="2">
...
当有很多myRadio的时候,如何直接得到所选radio的值,不用循环。
---------------------------------------------------------------
将radio放在span或div内,检查它的onclick事件
<span οnclick="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>
方法2.
<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 οnclick=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>