[英]hide/show checkbox once other checkbox is checked with a condition on its value
I am trying to hide a set of checkbox when one of these is checked, and make them reappear once it is not checked. 我试图在选中其中一个复选框时隐藏一组复选框,并在未选中时重新显示它们。 The conditions must be based on their value . 条件必须基于其值 。
var input = document.getElementsByTagName("input");
for(i=0;i<input.length;i++){
input[i].onchange = function(){
if(this.checked){
var value = $(this).val();
$("input[type=checkbox]:not(." + '60' + ")").hide();
$("input[type=checkbox]." + '60').show();
}
}
}
The group of checkbox to hide belong to the values 60 and 90 (two arms accessories can't be selected), the idea is that the two checkboxes cannot be selected, just one, so that's why I need to hide one of them once they're checked, and make them reappear once unchecked. 要隐藏的复选框组属于值60和90(无法选择两个武器附件),其想法是无法选择两个复选框,只能选择一个,因此这就是为什么我需要在其中隐藏一个复选框的原因被选中,并使其一旦取消选中就重新出现。
<input type="checkbox" value="50" /> conductive plastic foot cup $50<br/>
<input type="checkbox" value="60" /> <a>T-arms 2 $60</a><br/>
<input type="checkbox" value="90" /> <a>T-arms 1 $90</a><br/>
<input type="checkbox" value="80" /> metal rails $80<br/>
<input type="checkbox" value="30" /> plastic rails $30<br/>
<input type="checkbox" value="70" /> foot rest ring $70<br/>
<input type="checkbox" value="120" />plastic five star foot $120 <br/>
I'd suggest the following jQuery: 我建议使用以下jQuery:
$('.arms').change(
function(){
$('.arms').not(this).prop('disabled',this.checked);
});
Coupled with the amended HTML: 加上修改后的HTML:
<input id="check1" type="checkbox" value="50" />
<label for="check1">conductive plastic foot cup $50</label>
<input id="check2" type="checkbox" value="60" class="arms" />
<label for="check2">T-arms 2 $60</label>
<input id="check3" type="checkbox" value="90" class="arms" />
<label for="check3">T-arms 1 $90</label>
<input id="check4" type="checkbox" value="80" />
<label for="check4">metal rails $80</label>
<input id="check5" type="checkbox" value="30" />
<label for="check5">plastic rails $30</label>
<input id="check6" type="checkbox" value="70" />
<label for="check6">foot rest ring $70</label>
<input id="check7" type="checkbox" value="120" />
<label for="check7">plastic five star foot $120</label>
The above uses the label
element, with a for
attribute (or property) to associate the text explicitly with the relevant checkbox
. 上面使用带有for
属性(或属性)的label
元素将文本与相关的checkbox
显式关联。
I've added a class
to the input
s that are mutually-exclusive in order to easily target them with a selector, which sets the disabled
property of the other element (or elements if you add more mutually-exclusive checkboxes
) according to whether the checked, or unchecked, element is checked or unchecked. 我已经添加了class
的input
是相互排斥的,以便容易地与一个选择器,它设定目标文件S disabled
另一元件的特性(或元件如果添加更多互斥checkboxes
根据)是否已选中或未选中元素已选中或未选中。
Incidentally, it's usually better to disable form-fields, rather than removing/hiding them, that way if a user clicks by accident they're not left wondering where the other option's gone to, or surprised when it suddenly reappears. 顺便说一句,禁用表单域通常比删除/隐藏表单域更好,这样一来,如果用户无意中单击,他们就不会怀疑另一个选项的去向,也不会惊讶于另一个选项突然出现。
If, however, you really want to show/hide the 'other' element(s), then you can use the following: 但是,如果您确实要显示/隐藏“其他”元素,则可以使用以下内容:
$('.arms').change(
function(){
$('.arms').not(this).add($(this).next())[this.checked ? 'hide' : 'show']();
});
Or you can even use the first approach (using the prop('disabled',this.checked)
) with the following CSS (in compliant browsers that implement the :disabled
pseudo-selector: 或者甚至可以将第一种方法(使用prop('disabled',this.checked)
)与以下CSS结合使用(在实现:disabled
伪选择器的兼容浏览器中):
input:disabled,
input:disabled + label {
display: none;
}
References: 参考文献:
I love the use of data
attributes for such kind of stuff: 我喜欢将data
属性用于此类工作:
<input type="checkbox" value="50" /> conductive plastic foot cup $50<br/>
<input type="checkbox" value="60" data-exclude="input[value=90]" /> T-arms 2 $60<br/>
<input type="checkbox" value="90" data-exclude="input[value=60]" /> T-arms 1 $90<br/>
<input type="checkbox" value="80" /> metal rails $80<br/>
<input type="checkbox" value="30" /> plastic rails $30<br/>
<input type="checkbox" value="70" /> foot rest ring $70<br/>
<input type="checkbox" value="120" />plastic five star foot $120 <br/>
var input = document.getElementsByTagName("input");
$('input').each(function() {
var $input = $(this);
$input.change(function() {
var $exclude = $($input.data('exclude'));
$exclude.toggle(! $input.is(':checked'));
});
});
http://jsfiddle.net/bikeshedder/2H5kB/10/ http://jsfiddle.net/bikeshedder/2H5kB/10/
btw. 顺便说一句 I'd rather disable the checkbox than hiding it. 我宁愿禁用该复选框,也不愿隐藏它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.