[java/VB script] Javascript 如何得到input type="radio" 的值?



[java/VB script] Javascript 如何得到input type="radio" 的值?

方法1:
  1. <script   language="JavaScript">   
  2.   <!--   
  3.   function   chk(){   
  4.   var   obj   =   document.getElementsByName('imgposition');   
  5.   for   (i=0;i<obj.length;i++){   
  6.   if   (obj[i].checked){   
  7.   alert(obj[i].value);   
  8.   return;   
  9.   }   
  10.   }   
  11.   alert('你沒選');   
  12.   }   
  13.   //-->   
  14.   </script>   
  15.   <form   name=form1>   
  16.   <input   type="radio"   name="imgposition"   value="1">   
  17.   <input   type="radio"   name="imgposition"   value="2">   
  18.   <input   type="radio"   name="imgposition"   value="3">   
  19.   </form>   
  20.   <input   type="button"   onclick="chk()">   
複製代碼
方法2:
  1. <INPUT type=radio name="radio" CHECKED>1-10 years old
  2. <INPUT type=radio name="radio">11 years old
  3. <INPUT type=radio name="radio">12-120 years old
  4. This example uses script to detect which radio button the user selects.

  5. <SCRIPT>
  6. function detect()
  7. {
  8.     if (radio[0].checked)
  9.         alert("You're between 1 and 10 years old.")
  10.     else if (radio[1].checked)
  11.         alert("You're 11 years old.")
  12.     else
  13.         alert("You're between 12 and 120 years old.")
  14. }
  15. </SCRIPT>
複製代碼