其實js 獲取radio的值是很簡單的,如果是選項按鈕組的話就遍歷一次就行了,如果是單選的就直接判斷.value就行了。
<!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=gb2312" />
<title>js 獲取radio的值</title>
<script>
function radiogroup()
{
for(var i=0;i<formobj.a.length;i++){
if(formobj.a[i].checked){
alert("單選框選中: " + formobj.a[i].value);
return true;
}
}
alert("單選框未選中");
return false;
}
</script>
實例二
<script>
function search(obj)
{
var urlparam
for(i=0;i<obj.getelementsbytagname("input").length-1;i++)
{
if(obj.getelementsbytagname("input")[i].checked)
{
urlparam=obj.getelementsbytagname("input")[i].value
break
}
}
obj.action+="&sjprice="+urlparam
return true
}
</script>
</head>
<body>
<form id="form" name="form" method="post" action="">
<p>
& nbsp; <label>
<input type="radio" name="radiogroup1[]" value="1" id=" radiogroup1_0" />
單選</label>
<br />
<label>
<input type="radio" name="radiogroup1[]" value="1" id="radiogroup1_1" />
單選</label>
<br />
<label>
<input type="radio" name=" radiogroup1[]" value="1" id="radiogroup1_2" />
單選</label>
<br />
<label>
<input type=" radio" name="radiogroup1[]" value="1" id="radiogroup1_3" />
單選</label>
</p>
<p>
<input type="button" name=" button" id="button" value="檢測" onclick="網頁特效:radiogroup();" />
<br />
</p>
</form>
</body>
</html>
方法三
一段js獲取radio一組選中的值的代碼,發出來給大家分享一下!
預覽函數
var colorid="";
function showtemp()
{
var eless = document.getelementsbyname("colorstyle");
for(var i=0;i<eless.length;i++)
{
alert(eless[i].checked+eless[i].name+eless[i].id);
if(eless[i].checked)
{
colorid=eless[i].value;
break;
}
}
alert(colorid+"--");
}
方法四,一段完整的實例
<script lanage="javascript">
function isid()
{
alert(document.all.gender.value);
if(document.all.gender.value='new')
{
eval("document.all.emp.style.display='block' ")
return false;
}else
{
eval("document.all.emp.style.display='none' ")
return true;
}
}
function foo()
{
var selectedindex = -1;
var form1 = document.getelem entbyid("form1");
var i = 0;
for (i=0; i<form1.gender.length; i++)
{
& nbsp; if (form1.gender[i].checked)
{
selectedindex = i;
alert("您選擇項的 value 是:" + form1.gender[i].value);
break;
}
}
if (selectedindex < 0)
{
alert("您沒有選擇任何項");
}
}
</script>
<body>
<form method="post">
<table>
<tr><td>
<input type ="radio" value="new" />
新員工
<input type="radio" value="old" checked ="checked"/>
老員工
<input type="text" />
</td>
< /tr>
</table>
</form>
</body>
</html>