我需要一个单选按钮的事件触发器,当它被取消选中时,因为检查了另一个按钮.
下面的代码应该证明我的困境.
如果你注意到,有一个onchange事件触发器附加到每个单选按钮和html代码中的复选框.从理论上讲,状态从检查到未检查的变化应该触发onchange事件.
这与复选框的预期一致.如果您选中一个,则会收到警告"您的项目已更改为已选中".如果取消选中它,则会收到警告"您的项目已更改为未选中".
使用单选按钮,当您选中按钮1时,您会按预期获得警报"您的项目已更改为已选中",因为该按钮已从未选中更改为已选中.但是,当您选中第二个按钮并且第一个单选按钮从已选中更改为未选中时,"onchange"事件触发器不会触发,并且不会触发"其他"警报.
所以对我来说这个问题是当一个单选按钮被另一个被检查的按钮取消选中时会触发什么事件?
我感谢大家对此的帮助.
--Kenoli
<html>
<head>
<title></title>
<script type="text/javascript">
function clickAction() {
//alert ('Your item changed');
if (this.checked == true) {alert ('Your item is changed to checked');}
else if (this.checked == false) {alert('Your item is changed to unchecked');}
}
</script>
<script type="text/javascript">
function initializeToggles() {
var button1= document.getElementById('button1');
button1.onchange = clickAction;
var box1= document.getElementById('box1');
box1.onchange = clickAction;
var box2= document.getElementById('box2');
box2.onchange = clickAction;
}
</script>
<script type="text/javascript">window.onload = initializeToggles;</script>
</head>
<body>
<input type="radio" name="testRadio" id="button1" >Button …Run Code Online (Sandbox Code Playgroud) javascript ×1