gpt4 book ai didi

jquery - 如何按类和属性值过滤掉?

转载 作者:行者123 更新时间:2023-12-01 06:55:56 25 4
gpt4 key购买 nike

我使用的是 jQuery 1.4.3

我有几个如下所示的输入:

 <input type='checkbox' id='1' class='Product' comp='1'>
<input type='checkbox' id='2' class='Product' comp='1'>
<input type='checkbox' id='3' class='Product' comp='1'>
<input type='checkbox' id='4' class='Product' comp='2'>
<input type='checkbox' id='5' class='Product' comp='2'>
<input type='checkbox' id='6' class='Product' comp='2'>

如果单击具有产品类别的框,我想获取其补偿值并取消选中具有不同补偿值的其他框。

通常,我会循环检查每个比较值,然后在必要时取消选中,如以下伪代码:

ThisComp == 1 // comp value of checkbox that was just clicked
$(".Product").each(function() {
var Comp $(this).attr("comp");
if (ThisComp != Comp) {
// $(this).attr("checked", false);
}
});

我想知道是否有一种方法可以使用选择器中的 comp 值进行过滤,例如这个伪代码:

$(".Product comp=2").attr("checked", false);

此处的目标是以有效的方式取消选中与点击的补偿值不同的任何框。

最佳答案

这个怎么样:

$( '#boxes' ).delegate('input:checkbox', 'click', function () {
var comp = +$( this ).attr( 'comp' );
$( this ).siblings().not( '[comp="' + comp + '"]' ).prop( 'checked', false );
});

现场演示: http://jsfiddle.net/BMtTy/

<小时/>

或者使用数据属性:

$( '#boxes' ).delegate('input:checkbox', 'click', function () {
var comp = +$( this ).data( 'comp' );
$( this ).siblings().not( '[data-comp="' + comp + '"]' ).prop( 'checked', false );
});

现场演示: http://jsfiddle.net/BMtTy/1/

关于jquery - 如何按类和属性值过滤掉?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9182111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com