现在的位置: 首页 > 综合 > 正文

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

2013年08月21日 ⁄ 综合 ⁄ 共 1165字 ⁄ 字号 评论关闭
六年之前我在做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="提交" onclick="abc()"/>

</form>
</body>
</html>

抱歉!评论已关闭.