html中我想用js获取radio选中的值,怎么不对啊,求大神指教
<!doctypehtml><htmllang="en"><head><metacharset="UTF-8"><metaname="Generator"content=...
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script>
function check(){
var r2 = document.getElementsByName("radio");
var values="";
for(var i = 0;i<r2.length;i++)
{
if(r2[i].checked==true){
values=r2[i].value;
alert(values);
}
}
}
</script>
</head>
<body>
<form>
5分<input name="r2" type="radio" value="5" onclick="check()"/>
4分<input name="r2" type="radio" value="4" onclick="check()"/>
3分<input name="r2" type="radio" value="3" onclick="check()"/>
2分<input name="r2" type="radio" value="2" onclick="check()"/>
1分<input name="r2" type="radio" value="1" onclick="check()"/>
</form>
</body>
</html> 展开
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script>
function check(){
var r2 = document.getElementsByName("radio");
var values="";
for(var i = 0;i<r2.length;i++)
{
if(r2[i].checked==true){
values=r2[i].value;
alert(values);
}
}
}
</script>
</head>
<body>
<form>
5分<input name="r2" type="radio" value="5" onclick="check()"/>
4分<input name="r2" type="radio" value="4" onclick="check()"/>
3分<input name="r2" type="radio" value="3" onclick="check()"/>
2分<input name="r2" type="radio" value="2" onclick="check()"/>
1分<input name="r2" type="radio" value="1" onclick="check()"/>
</form>
</body>
</html> 展开
1个回答
展开全部
getElementsByName("radio");
改为
getElementsByName("r2");
要仔细哦
还可以换种方法实现
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script>
function check(e){
alert(e.value)
}
</script>
</head>
<body>
<form>
5分<input name="r2" type="radio" value="5" onclick="check(this)"/>
4分<input name="r2" type="radio" value="4" onclick="check(this)"/>
3分<input name="r2" type="radio" value="3" onclick="check(this)"/>
2分<input name="r2" type="radio" value="2" onclick="check(this)"/>
1分<input name="r2" type="radio" value="1" onclick="check(this)"/>
</form>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询