这是我的 jQuery 脚本:
$("input[type=radio]:checked").live("click", function() {
$(this).attr("checked",false);
});
我想让我所有的单选按钮都可以选中和取消选中。该代码假设仅在单击选中的收音机时触发。但不知何故,任何无线电点击(无论是否选中)都会触发此事件。就好像浏览器会先检查收音机然后运行我的脚本。
如果 I alert($("input[type=radio]:checked").size())
,它给了我正确的计数。
我正在测试 FF4 中的代码,但我希望该解决方案也能满足 IE 的需求。
好的,由于我的问题似乎令人困惑,这是我想要的工作代码,但它需要额外的自定义属性,我希望可以避免。
<input type="radio" name="group1" value="1" isChecked="false"/>
<input type="radio" name="group1" value="2" isChecked="false"/>
<input type="radio" name="group1" value="3" isChecked="false"/>
<script>
$(document).ready(function(){
$("radio").click(function(){
if($(this).attr("isChecked")=="false"){
$(this).attr("isChecked","true");
$(this).attr("checked",true);
}
else{
$(this).attr("isChecked","false");
$(this).attr("checked",false);
}
});
});
</script>