首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

怎么获得所选radio的值

2012-09-05 
如何获得所选radio的值方法1.inputtyperadionamemyRadiovalue1inputtyperadionamemyRadi

如何获得所选radio的值
方法1.

<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> 


方法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  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> 

热点排行