简体   繁体   English

如何使用jQuery选择具有特定属性唯一值的每个复选框的第一次出现?

[英]How can I select the first occurence of each checkbox that has a unique value for a specific attribute using jQuery?

Hard question to understand, I'm sure, so I'll show you what I'm trying to do: 我敢肯定,这是一个很难理解的问题,所以我将向您展示我正在尝试做的事情:

Let's say I have the following checkboxes HTML: 假设我有以下复选框HTML:

<input type="checkbox" id="selectAll" /> Select All
<input type="checkbox" name="addressId" value="100" uid="1" /> Address 100
<input type="checkbox" name="addressId" value="101" uid="2" /> Address 101
<input type="checkbox" name="addressId" value="102" uid="3" /> Address 102
<input type="checkbox" name="addressId" value="103" uid="4" /> Address 103
<input type="checkbox" name="addressId" value="104" uid="2" /> Address 104
<input type="checkbox" name="addressId" value="105" uid="4" /> Address 105
<input type="checkbox" name="addressId" value="106" uid="5" /> Address 106

I am currently using the following Select All jQuery logic: 我目前正在使用以下“全选jQuery”逻辑:

$("#selectAll").change(function() {
    if ($(this).is(":checked"))
        $("input[name='addressId']").attr("checked", "").removeAttr("checked").attr("checked", "checked");
    else
        $("input[name='addressId']").attr("checked", "").removeAttr("checked");
});

Basically, I need to update this logic so that only the FIRST checkbox of each set of checkboxes that has a matching uid value (for eaxmple, address 101 and 104 have a matching uid value -2) is selected. 基本上,我需要更新此逻辑,以便仅选择具有匹配的uid值(对于eaxmple,地址101和104具有匹配的uid值-2)的每组复选框的FIRST复选框。 So, in the above example, only Address Ids 100, 101, 102, 103, AND 106 would be selected, and 104 and 105 would remain unselected. 因此,在上面的示例中,仅地址ID 100、101、102、103和AND 106将被选中,而104和105将保持未选中状态。 How I can do this in jQuery? 我如何在jQuery中做到这一点? I just can't seem to figure out the selector! 我似乎无法弄清楚选择器!

Thanks. 谢谢。

I'm not sure if this is the best way of getting the checkboxes, but it works: 我不确定这是否是获取复选框的最佳方法,但是它可以工作:

var uids = [],
    unique = $('input[name=addressId]').filter(function(index) {
        var uid = $(this).attr('uid');
        if ($.inArray(uid, uids) == -1) {
            uids.push(uid);
            return true;
        } else {
            return false;
        }
    });

So then, you could simplify the event handler on #selectAll as: 因此,您可以将#selectAll上的事件处理程序简化为:

$('#selectAll').change(function() {
    unique.attr('checked', this.checked);
});

Working example: http://jsfiddle.net/FishBasketGordo/crkA7/ 工作示例: http : //jsfiddle.net/FishBasketGordo/crkA7/

Looks like this'll do the job: 看起来这可以完成工作:

$("#selectAll").change(function() {
    if ($(this).is(":checked"))
    {
        var checkedUids = [];
        $("input[name='addressId']").each(function(){
           var $this = $(this);
           if(checkedUids.indexOf($this.attr('uid')) == -1)
           {
              $this.attr("checked","checked");
             checkedUids.push(  $this.attr('uid') );
           }
        });
    }  
    else
        $("input[name='addressId']").attr("checked", "").removeAttr("checked");
});

Live example: http://jsfiddle.net/mJvHa/ 实时示例: http//jsfiddle.net/mJvHa/

$("#selectAll").change(function() {

    var $inputs = $("input[name='addressId']"),
        state = this.checked,
        UIDs = [];

    $.each($inputs.toArray().reverse(), function()
    {
        if ( $.inArray($(this).attr('uid'), UIDs) != -1 )
            $(this).prop('checked', state);
        else
            UIDs.push( $(this).attr('uid') );
    });
});

Here's the fiddle: http://jsfiddle.net/YEbD2/ 这是小提琴: http : //jsfiddle.net/YEbD2/

Here you go 干得好

Working Demo 工作演示

$("#selectAll").change(function() {
    if (this.checked){
        $("input[name='addressId']").each(function(i){
           if(i == 0 || $("input[name='addressId']").filter(":lt("+i+")").filter(")[uid="+$(this).attr('uid')+"]").length == 0){
              this.checked = true;
           }
            else{
                this.checked = false;
            } 
        });
    }
    else{
        $("input[name='addressId']").attr("checked", false).removeAttr("checked");
    }
});

You can use: 您可以使用:

$("#selectAll").change(function() {
if ($(this).is(":checked"))
    $("input[name='addressId']").filter('[uid=' + specific_uid_value + ']').first().attr("checked", "").removeAttr("checked").attr("checked", "checked");
else
    $("input[name='addressId']").filter('[uid=' + specific_uid_value + ']').first()..attr("checked", "").removeAttr("checked");
});

where specific_uid_value is the uid value that you want to filter. 其中specific_uid_value是要过滤的uid值。

the jQuery filter function will filter the checkboxes with attribute uid = your desider value and the jQUery first function will give you the first checkbox that fulfill the requirements. jQuery过滤器函数将过滤属性为uid =您的目标值的复选框,而jQUery first函数将为您提供满足要求的第一个复选框。

Hope it helps you. 希望对您有帮助。

This is one way to do it, but I feel like it could be better. 这是做到这一点的一种方法,但我认为它可能会更好。 After the conditional, loop through the inputs and uncheck any duplicates. 有条件后,遍历输入并取消选中任何重复项。

$("#selectAll").change(function() {
    if ($(this).is(":checked"))
        $("input[name='addressId']").attr("checked", "").removeAttr("checked").attr("checked", "checked");
    else
        $("input[name='addressId']").attr("checked", "").removeAttr("checked");

    var uids = new Array();

    $("input[name='addressId']").each(function(){
        if (jQuery.inArray($(this).attr('uid'), uids))
            $(this).removeAttr("checked");
        else
            uids($(this).attr('uid'));
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用jquery选择具有&#39;abbr&#39;属性特定值的元素 - How can I select element that has specific value for 'abbr' attribute with jquery 如何使用Javascript / Jquery为每个唯一属性值选择第一个匹配的元素? - How to select the first matched element for each unique attribute value with Javascript/Jquery? 每当用户使用jQuery在选择下拉列表中选择特定值时,如何执行功能? - How can I perform a function each time that the user select a specific value into a select dropdown using jQuery? jquery:如何选择属性名称中包含数组值的复选框? - jquery: How to select the checkbox that has an array value in its attribute name? 如何使用jQuery检查具有自定义属性和特定值的输入字段 - How can i check input field that has custom attribute with specific value using jQuery 如何使用jQuery选择具有属性和属性不等于特定值的元素? - How to select element has attribute and attribute not equal to specific value using jquery? 如果值两个循环是匹配值,如何将选中的属性支持到每个复选框中的复选框(使用 Jquery) - How to prop the attribute of checked to the checkbox in each checkbox if the value two loop is match value (Using Jquery) 使用数据属性和jQuery,如何通过数据属性名称和数据属性值选择元素 - Using data attributes and jQuery how can I select elements by data attribute name AND data attribute value jQuery +如何只选择每个元素的第一个实例? - jQuery + How can I select only the first instance of each element? 如何在div中选择具有JQUERY中特定属性的元素? - How can I select an element inside a div with a specific attribute in JQUERY?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM