如何用JavaScript取得Radio被選中的值

六年之前我在做Web,三年之前我厭煩了Web開發,最近有重新拾起Web開發,竟然忘了怎麼去Radio的值,呵呵。
  document.getElementById("radioName") .value竟然行不通,google一下才知道不是這麼寫。

下面是示例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<script language="javascript">
function   getRadioBoxValue(radioName) 
  { 
            var obj = document.getElementsByName(radioName);           
              for(i   =   0;   i   <   obj.length;   i++)    { 
                  if(obj[i].checked)    { 
                          return   obj[i].value; 
                  } 
              }         
             return "undefined";       
  }  

    function abc(){
        var    a = getRadioBoxValue("radiobutton");   
        alert(a);   
    }
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <label>
  <input type="radio" name="radiobutton" value="aa" />
  aa</label>
  <label>
  <input type="radio" name="radiobutton" value="bbb" checked/>
  bbb</label>
  <label>
  <input type="radio" name="radiobutton" value="ccc" />
  ccc</label>

  <input type="submit" name="Submit" value="提交" οnclick="abc()"/>

</form>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章