Ark*_*aar 2 html javascript jquery radio-button
对于一个项目,我需要在一个组中使用两个单选按钮,并且需要在单击时取消选中已选中的单选按钮.
我尝试过类似的东西,但这不起作用:
$(document).ready(function() {
$("input[type='radio']:checked").click(function(e) {
e.preventDefault();
$(this).prop("checked", false);
alert('Hello World!');
});
});
Run Code Online (Sandbox Code Playgroud)
单击选中的单选按钮时,它甚至不会发出警报.
每当我在我的控制台中调用它时:
$("input[type='radio']:checked").prop('checked', false);
Run Code Online (Sandbox Code Playgroud)
取消选中已选中的单选按钮,以便该部分可以正常工作.
实际上,当你使用click它时,不能以mousedown相反的方式使用它
附加工作片段
$(function(){
$('#wrapper').on('mousedown', 'input[type=radio]',function(e){
if(!$(e.target).is(':checked')){
return true;
}
e.preventDefault();
$(this).prop("checked", false);
alert('Hello World!');
});
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">
<input type=radio name=hello />
<input type=radio name=hello />
<input type=radio name=hello checked/>
</div>Run Code Online (Sandbox Code Playgroud)