代码之家  ›  专栏  ›  技术社区  ›  Max

突出显示HTML表的指定列上的值

  •  0
  • Max  · 技术社区  · 7 年前

    HTML表上的突出显示值有问题。我只需要突出显示指定列上的值,而不是整个表。任何 感谢您的帮助!谢谢。 https://jsfiddle.net/raphcLb0/

    下面是我使用的js代码:

    $(document).ready(function() {
    
        $('.selector').each(function() {
            $(this).on('click', check); 
        });
    
        $('.all').each(function() {
            $(this).on('click', all); 
        });
    
        function all(event) {
            if($(this).is(':checked')){  
                $("input:checkbox:not(:checked)",$(this).parents('form')).not(this).prop("checked","checked");
            } else {
                $("input:checkbox(:checked)",$(this).parents('form')).not(this).prop("checked","");
            }
    
            //$('.selector').prop("checked", this.name === "SelectAll");
            check(event);
        }
    
        function check(event) {
            var checked = $(".selector:checked").map(function () {
                return this.name
            }).get()
            $('td').removeClass("highlight").filter(function () {
                return $.inArray($(this).text(), checked) >= 0
            }).addClass("highlight")
            if ($(this).is(".selector"))
                $('.all').not(this).prop("checked", false)
        }
    
        $( "#Hidden").on( "click", function() {
             $(".selector").toggle();
        });
    
    });
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Jacques Marais    7 年前

    以下方法应该有效:

    // JavaScript Document
    $(document).ready(function() {
    
    $('.selector').each(function() {
        $(this).on('click', check); 
    });
        $('.all').each(function() {
           $(this).on('click', all); 
        });
    
    function all(event) {
    
            if($(this).is(':checked')){  $("input:checkbox:not(:checked)",$(this).parents('form')).not(this).prop("checked","checked");
        } else {
            $("input:checkbox(:checked)",$(this).parents('form')).not(this).prop("checked","");
        }
    
        //$('.selector').prop("checked", this.name === "SelectAll");
    
        check(event);
    }
    
    function check(event) {
        var checked = $(".selector:checked").map(function () {
            return this.name
        }).get()
        $('td').removeClass("highlight").filter(function () {
            return $.inArray($(this).text(), checked) >= 0 &&
                  $('#form' + ($(this).index() + 1)).find('[name="'+$(this).text()+'"]').is(":checked")
        }).addClass("highlight")
        if ($(this).is(".selector"))
            $('.all').not(this).prop("checked", false)
    
    }
    
    
     $( "#Hidden").on( "click", function() {
            $(".selector").toggle();
        });
    
    
    
    });
    

    Example

    我所做的是更正表单的ID并添加以下行:

    $('#form' + ($(this).index() + 1)).find('[name="'+$(this).text()+'"]').is(":checked")

    这样做的目的是选择包含当前 td 相对于ITS的元素 tr 起源。然后它会找到与 TD公司 的文本内容并检查是否选中。