js取radio值的问题?

shenxian5 2010-01-07 03:35:38
html:
<input name="abc-001-100" value="A" type="radio">
<input name="abc-001-100" value="B" type="radio">
<input name="abc-001-100" value="C" type="radio">
<input type="button" value="check" onclick="checkradio" />

Q:如何点击checkradio后,通过js,取得当前html中checked的radio的值?比如选择了第一个,则取到值A?
...全文
70 2 打赏 收藏 举报
写回复
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
jol_boy 2010-01-07
  • 打赏
  • 举报
回复

<!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 content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>无标题 1</title>
<script type="text/javascript" >
function checkradio()
{
var t=document.getElementsByName("abc-001-100");
for(var i=0;i<t.length;i++)
{
if(t[i].checked)
{
alert(t[i].value);
}
}
}
</script>
</head>

<body>
<input name="abc-001-100" value="A" type="radio">
<input name="abc-001-100" value="B" type="radio">
<input name="abc-001-100" value="C" type="radio">
<input type="button" value="check" onclick="checkradio()" />

</body>

</html>

sundotLei 2010-01-07
  • 打赏
  • 举报
回复

<SCRIPT LANGUAGE="JavaScript">
<!--
function checkradio(){
var o=document.getElementsByName("abc-001-100");
var v=null;
for(var i=0;i<o.length;i++){
if(o[i].checked){
v=o[i].value;
break;
}
}
if(v!=null)
alert('选择值为'+v);
else
alert('未选择');

}
//-->
</SCRIPT>
</HEAD>

<BODY>
<input name="abc-001-100" value="A" type="radio">
<input name="abc-001-100" value="B" type="radio">
<input name="abc-001-100" value="C" type="radio">
<input type="button" value="check" onclick="checkradio()" />

</BODY>
发帖
JavaScript

8.7w+

社区成员

Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
帖子事件
创建了帖子
2010-01-07 03:35
社区公告
暂无公告