单击时取消选择单选按钮
Unselect radio button on click
我试图用jquery点击来取消选择单选按钮。
通缉行为:
- 如果已选中单击的单选按钮,请取消选中
- 如果未选中单击的单选按钮,请选中
这是我的小提琴:http://jsfiddle.net/2te28/15/
和我的代码:
$('.table').on('click mousedown', '.radio', function() {
if($(this).attr('id') == $(this).parents('tr').find('.radio:checked').attr('id'))
$(this).removeAttr('checked');
});
我的问题是,无论我点击什么,它总是被取消选中。
编辑:已更正id转录错误
编辑2:不能同时选择两个单选按钮
更改HTML。您可以更改id
,但也必须更改name
。
$('.table').on('click', '.radio', function() {
if (this.getAttribute('checked')) { // check the presence of checked attr
$(this).removeAttr('checked'); // if present remove that
} else {
$(this).attr('checked', true); // if not then make checked
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table">
<tr>
<td>
<input type="radio" id="radio1" name="1" checked="checked" class="radio" />
<input type="radio" id="radio2" name="2" checked="checked" class="radio" />
</td>
</tr>
</table>
演示
根据一条评论
$('.table').on('click', '.radio', function() {
if (this.getAttribute('checked')) {
$(this).removeAttr('checked');
$(this).siblings('.radio').attr('checked', false);
} else {
$(this).attr('checked', true);
$(this).siblings('.radio').removeAttr('checked');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table">
<tr>
<td>
<input type="radio" id="radio1" name="1" checked="checked" class="radio" />
<input type="radio" id="radio2" name="2" checked="checked" class="radio" />
</td>
</tr>
</table>
DEMO
查看此演示:http://jsfiddle.net/535dR/1/
代码:
$('.table input[type="radio"]').on('mouseup', function() {
var radio = $(this);
if(radio.prop('nc') == 0) {
setTimeout(function() {
// *to work: this needs to run only after the event completes*
if(radio.is(':checked')) radio.removeAttr('checked');
radio.prop('nc', 1);
}, 10);
}
else radio.prop('nc', 0);
});
更新的演示:http://jsfiddle.net/535dR/3/
评论中提到的这两点都得到了解决。
这里有一个简单的工作JSFiddle:http://jsfiddle.net/2te28/61/
代表单选按钮的html标签的<id>
和<name>
必须是唯一的,因此更改为:
<input type="radio" id="radio1" name="1" class="radio"/>
<input type="radio" id="radio2" name="2" class="radio"/>
然后,您可以添加一些简单的JQuery代码:
var radioChecked = $('.table .radio').is(':checked');
$('.table .radio').click(function() {
radioChecked = !radioChecked;
$(this).attr('checked', radioChecked);
});
<input type="radio" id="radio1" name="1" class="radio"/>
<input type="radio" id="radio2" name="2" class="radio"/>
那么你只需要添加
$('.radio').click(function(){
value = $(this).val();
if(val == 1){
$('#radio1').removeAttr('checked');
}
else{
$('#radio2').removeAttr('checked');
}
});
这对我有效
我所做的非常简单。
我一开始就设置了RadioButton.style.textDecoration = "none"
。这对RadioButton
元素没有任何影响,但设置仍然有效。
然后,在OnClick
事件上,我检查textDecoration
。如果是"none"
,我设置RadioButton.checked = true
和textDecoration = "underline"
如果是"underline"
,我设置RadioButton.checked = false
和textDecoration = "none"
相关文章:
- 根据页面加载时的单选按钮选择显示某些字段
- Ng点击记住单选按钮选择
- Angular未更新单选按钮选择
- 阅读JS中的单选按钮选择
- JQuery IE11 单选按钮选择器
- 确认单选按钮选择
- 贝宝表单问题的单选按钮选择
- 使用javascript在单选按钮选择上加载URL
- 显示基于多个单选按钮选择的图像
- 基于输入值的单选按钮选择
- 将域类id返回到由单选按钮选择的控制器
- 无法更改单选按钮选择 - KnockoutJS
- 单选按钮选择列表框更改并将 NULL 值传递给每个列表框
- 用户在Javascript的帮助下,仅通过单选按钮选择一个付款方式(信用卡)
- 基于多个单选按钮选择显示或隐藏元素
- 如何根据单选按钮选择在表中切换多个输入
- 无法更改单选按钮选择
- 提交页面而不为所需单选按钮选择值
- 使用基于单选按钮选择的 jquery 将表格单元格添加到行尾
- j查询动态验证单选按钮选择