<input type="radio" id="radioId" value="1" >
选中单选框JS代码:
var target = document.getElementById("radioId")
target.checked = true
附完整代码:
<!DOCTYPE html>
<!--STATUS OK-->
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="content-type" content="text/htmlcharset=gbk" />
<meta property="wb:webmaster" content="3aababe5ed22e23c" />
<meta name="referrer" content="always" />
<title>demo</title>
</head>
<body>
<input type="radio" value="radio1" id="radio1" name="radio"/><label for="radio1">RADIO 1</label>
<input type="radio" value="radio2" id="radio2" name="radio"/><label for="radio2">RADIO 2</label>
<input type="radio" value="radio3" id="radio3" name="radio"/><label for="radio3">RADIO 3</label>
<input type="radio" value="radio4" id="radio4" name="radio"/><label for="radio4">RADIO 4</label>
<br/><br/>
<button onclick="selectRadio('radio1')">选中radio1</button>
<button onclick="selectRadio('radio2')">选中radio2</button>
<button onclick="selectRadio('radio3')">选中radio3</button>
<button onclick="selectRadio('radio4')">选中radio4</button>
<br/>
<br/>
</body>
<script type="text/javascript">
function selectRadio(radioId) {
var target = document.getElementById(radioId)
target.checked = true
}
</script>
</html>
当<input type=radio name=radio_name value=radiobutton(单选框) 是动态加入页面时,它的数量有可能为0,1,或者大于1,因此在js 中需要判断单选框是否选中时,就必须循环判断每个单选框的状态,用var obj=document.getelementsbyname('audit_result')就可以得到单选框组合,并可通过obj.length得到它的长度,这样就不用判断它的数量是0,1,或者大于1<form name=myform action= method= onsubmit=return check()
<input type=radio name=picno value=1
<input type=radio name=picno value=2
<input type=radio name=picno value=3
<input type=submit value=submit</form
<script language=网页特效
function check()...{var flag=0
for(i=0i<myform.picno.lengthi++)
if(myform.picno[i].checked==true)...{flag=1break}
if(!flag)...{
alert(请先选择更换图片的位置)
return false}}
</script方法二<script language=javascript
function checkvalue(){
var obj=document.getelementsbyname(radio_name)
//alert(obj.length)
for (var i=0i<obj.lengthi++){
if(obj[i].checked)
return true}
alert(没有选中!)
return false}
而用var ovar obj=document.all(radio_name)时,当数量为0或1时,obj就是undefined。