Javascript 取消选中单选按钮 考试 旅行 $(“输入[name=things]”。单击(函数(){ 如果($(this).is(':checked')){ $(this.removeAttr('选中') } })
这不管用。我怎样才能让它工作?下面是我希望它运行的方式:如果我单击选中的输入,那么这应该是未选中的,但是如果我单击未选中的输入,那么这应该被选中,并且取消选中其他输入Javascript 取消选中单选按钮 考试 旅行 $(“输入[name=things]”。单击(函数(){ 如果($(this).is(':checked')){ $(this.removeAttr('选中') } }),javascript,jquery,html,Javascript,Jquery,Html,这不管用。我怎样才能让它工作?下面是我希望它运行的方式:如果我单击选中的输入,那么这应该是未选中的,但是如果我单击未选中的输入,那么这应该被选中,并且取消选中其他输入 现场直播:这应该能帮你解决问题: 或者(普通js) 或(jQuery) 希望对您有所帮助:)单选按钮的设计使组中的一个成员始终处于选中状态。如果有三个选项,则应具有三个单选按钮: $(this).prop('checked', false); // Note that the pre-jQuery 1.6 idiom was /
现场直播:这应该能帮你解决问题: 或者(普通js) 或(jQuery)
希望对您有所帮助:)单选按钮的设计使组中的一个成员始终处于选中状态。如果有三个选项,则应具有三个单选按钮:
$(this).prop('checked', false);
// Note that the pre-jQuery 1.6 idiom was
// $(this).attr('checked', false);
考试
旅行
也不
有一些方法可以绕过这一点,但并不可靠,也不能不违反用户对UI工作方式的期望。
使用实时功能。。处理程序始终与事件保持连接。我想出了以下代码:
<input type="radio" name="things" value="exam" id="things_exam">
<label for="things_exam">exam</label>
<input type="radio" name="things" value="journey" id="things_journey">
<label for="things_journey">journey</label>
<input type="radio" name="things" value="" id="things_neither" checked><!-- note checked attribute, this is the default, one member of the group should always be checked -->
<label for="things_neither">neither</label>
不幸的是,我不得不使用额外的变量来存储checked属性,因为当触发click事件时,checked
属性总是true
我知道这个问题是在2月份提出的,但我发现这个信息很有趣,我想与大家分享: 单选按钮 单选按钮与复选框类似,不同之处在于当几个按钮共享相同的控件名称时,它们是互斥的:当其中一个按钮处于“打开”状态时,具有相同名称的所有其他按钮处于“关闭”状态。输入元素用于创建单选按钮控件 如果共享同一控件名称的集合中没有单选按钮最初为“开”,则未定义用于选择哪个控件最初为“开”的用户代理行为。注意。由于现有实现对这种情况的处理方式不同,因此当前规范不同于RFC 1866([RFC1866]第8.1.2.4节),RFC 1866规定:在任何时候,都只检查一组单选按钮中的一个。如果一组单选按钮的任何元素均未指定“已选中”,则用户代理必须首先选中该组的第一个单选按钮 由于用户代理行为不同,作者应该确保在每一组单选按钮中,一个按钮最初处于“打开”状态 这里有一个不错的技巧:
var prev = {};
$("input[name=things]").click(function(){
if (prev && prev.value == this.value) {
$(this).prop('checked', !prev.status);
}
prev = {
value: this.value,
status: this.checked
};
});
这段代码应该像HTML一样工作,但是,如果需要,您也可以在javascript或jquery中附加附加附加逻辑的事件。和Hi的可能重复您是说,如果您单击文本考试或旅程,则应选择或取消选择相应的输入?如果您发现问题,则可能重复标记为重复,但是不要复制其他答案中的粘贴内容。它不起作用,原因如下:我将检查已检查的收音机并取消检查。对不起,我不知道怎么做。现在就做,我的坏。如果我再次单击选定的收音机,则仍会选中此选项。应该不勾选,那样不行。。只有当表单元素更新到服务器时,选中的值才会更新。不可能取消选中选中的收音机?@GryzOshea-请参阅我答案的最后一段。你不能可靠地做到这一点,即使你能做到,尝试也是没有意义的。
<input type="radio" name="Name1" value="Value1" onmousedown="this.c=this.checked" onclick="if (this.c) { this.checked
= false }" />
var prev = {};
$("input[name=things]").click(function(){
if (prev && prev.value == this.value) {
$(this).prop('checked', !prev.status);
}
prev = {
value: this.value,
status: this.checked
};
});
<input type="radio" name="Name1" value="Value1" onmousedown="this.c=this.checked" onclick="if (this.c) { this.checked
= false }" />
//I came up with something similar to the very first answer, over night.
//I use dirtyForms for JQuery inorder to monitor when I should forget to
//fill in some forms. https://github.com/snikch/jquery.dirtyforms
var prevFuncScope = {};
$("input[type=radio]").on('click', function(e) {
//$("input[type=radio]").click(function(){
let prevBlockScope = {
value: this.value,
status: this.checked
};
var $form = $('form#patient_entrance_form');
var $submitResetButtons = $form.find('[type="reset"],[type="submit"]');
//if ($(e).dirtyForms('isDirty')) $(e).dirtyForms('setClean'); //setClean();
//console.log('form is = ' + $('form#patient_entrance_form').dirtyForms('isDirty'));
//console.log('input[yes] = ' + $('#prev_chiro_care_yes').dirtyForms('isDirty'));
//console.log('input[no] = ' + $('#prev_chiro_care_no').dirtyForms('isDirty'));
//console.log($(this));
if (prevBlockScope.status == prevFuncScope.status) {
$("input[type=radio]").dirtyForms('setClean');
//$('#prev_chiro_care_yes').dirtyForms('setClean'); $(e).dirtyForms('setClean');
//$('#prev_chiro_care_no').dirtyForms('setClean');
//console.log('form is = ');
$(this).prop('checked', !prevBlockScope.status);
if ($('form#patient_entrance_form').dirtyForms('isDirty')) {
//console.log('dirty1');
$submitResetButtons.removeAttr('disabled');
} else if(!$(e).dirtyForms('isDirty')) {
//console.log('clean1');
$submitResetButtons.attr('disabled', 'disabled');
}
} else if (prevFuncScope && prevFuncScope.value == this.value) {
$("input[type=radio]").dirtyForms('setClean');
//$('#prev_chiro_care_yes').dirtyForms('setClean'); // $(e).dirtyForms('setClean');
//$('#prev_chiro_care_no').dirtyForms('setClean');
//console.log('form is = ');
$(this).prop('checked', prevBlockScope.status);
if ($('form#patient_entrance_form').dirtyForms('isDirty')) {
//console.log('dirty2');
$submitResetButtons.removeAttr('disabled'); //
} else if(!$(e).dirtyForms('isDirty')) {
//console.log('clean2');
$submitResetButtons.removeAttr('disabled');
}
}
prevFuncScope = {
value: this.value,
status: this.checked
//isChecked: $('form#patient_entrance_form').dirtyForms('isDirty')
};
});