求助HTML高手radio如何响应失去选中事件
1个回答
展开全部
用一个全局变量记住当前选项
<html>
<script language="javascript" defer="defer">
var cur;
function check(){
var val;
var rds = document.forms[0].rd;
for(var i=0;i<rds.length;i++){
if(rds[i].checked){
val = i;
break;
}
}
if(val != cur){cur = val; alert("changed")}
}
window.onload=function(){
var rds = document.forms[0].rd;
for(var i=0;i<rds.length;i++){
if(rds[i].checked){
cur = i;
break;
}
}
}
</script>
<body>
<form>
<input type="radio" name="rd" value="0" checked="checked" onclick="check()"> 0<br>
<input type="radio" name="rd" value="1" onclick="check()"> 1<br>
<input type="radio" name="rd" value="2" onclick="check()"> 2<br>
</form>
</body>
</html>
<html>
<script language="javascript" defer="defer">
var cur;
function check(){
var val;
var rds = document.forms[0].rd;
for(var i=0;i<rds.length;i++){
if(rds[i].checked){
val = i;
break;
}
}
if(val != cur){cur = val; alert("changed")}
}
window.onload=function(){
var rds = document.forms[0].rd;
for(var i=0;i<rds.length;i++){
if(rds[i].checked){
cur = i;
break;
}
}
}
</script>
<body>
<form>
<input type="radio" name="rd" value="0" checked="checked" onclick="check()"> 0<br>
<input type="radio" name="rd" value="1" onclick="check()"> 1<br>
<input type="radio" name="rd" value="2" onclick="check()"> 2<br>
</form>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询