【问题标题】:Highlight table row if cell duplicate如果单元格重复,突出显示表格行
【发布时间】:2012-04-17 21:16:59
【问题描述】:

我目前有下表:

Checkbox (ID)  |  Training Course Code  |  Training Course Name  |  ....
   [ ]         |  DE00155115-1          |  H&S Exec 1            |  ....
   [ ]         |  DE00155115-1          |  H&S Exec 1            |  ....
   [ ]         |  DE00074454-2          |  H&S Exec 2            |  ....

每门课程运行不止一次,但只能选择一次,因此每个课程代码有多个 ID。

我想要做的是能够在选中重复行后禁用/突出显示重复行。

在上面的示例中,如果选择了第一行,则第二行将禁用,反之亦然。如果选择了第三行,则不会发生任何事情。

我尝试了几个使用 jQuery 的函数,但不确定从哪里开始和构建。

【问题讨论】:

  • 多个同名id是无效的html,建议你改用class

标签: jquery jquery-ui


【解决方案1】:

这是一个冗长且稍微复杂的选项:

$('table')
    .on('click','input:checkbox',
        function(){
            var that = $(this),
                thatRow = that.closest('tr'),
                id = that.parent().next('td').text();

            if (that.is(':checked')){
                that
                    .closest('tbody')
                    .find('tr td:nth-child(2)')
                    .each(
                        function(){
                            if ($(this).text() == id){
                                $(this)
                                    .closest('tr')
                                    .not(thatRow)
                                    .addClass('disabled')
                                    .find('input:checkbox')
                                    .prop('disabled',true);
                            }
                        });
            }
            else {
                that
                    .closest('tbody')
                    .find('tr td:nth-child(2)')
                    .each(
                        function(){
                            if ($(this).text() == id){
                                $(this)
                                    .closest('tr')
                                    .not(thatRow)
                                    .removeClass('disabled')
                                    .find('input:checkbox')
                                    .prop('disabled',false);
                            }
                        });
            }
        });​

JS Fiddle demo.

参考资料:

【讨论】:

  • 非常详细的示例与参考谢谢!为了将来参考,是否可以在同一页面上禁用跨 2 个表的行?例如,如果课程日期按月划分,您能否禁用所有单独表格中的一行?
  • 当然,您只需要某种方式来选择两个(或所有)表即可。
  • 听起来不错,谢谢。你有一个例子或任何指针从哪里开始阅读?再次感谢:)
  • $('table').find('tbody tr td:nth-child(2)')替换that.closest('tbody').find('tr td:nth-child(2)')JS Fiddle demo
  • 感谢您的帮助 :) 非常感谢
【解决方案2】:

如果我理解正确,那么您只需要为下面的复选框实现一个点击处理程序,

DEMO

$('#courses > tbody > tr > td > .selectCourse').on('click', function () {
    var $tr = $(this).closest('tr')
    var selCourseCode = $tr.find('td:eq(1)').html();
    var isChecked = this.checked;
    if (isChecked) {
        $tr.addClass('highlight');
    } else {
        $tr.removeClass('highlight');
    }

    $.each ($tr.siblings(), function () {
        var courseCode = $(this).find('td:eq(1)').html();
        if (courseCode == selCourseCode) {
            if (isChecked) {
                $(this).find('.selectCourse').prop('disabled', true);
                $(this).addClass('disable');
            } else {
                $(this).find('.selectCourse').removeProp('disabled');
                $(this).removeClass('disable');
            }
        }
    });
});

CSS:

.highlight { background-color: #386C98; }
.disable { background-color: #386C98; color: grey; }

【讨论】:

  • 看起来你打败了我,但不是很多。非常相似的方法。
  • 看看不同的方法非常有用,谢谢:)
【解决方案3】:

这是一个示例 jQuery 函数,它应该在旧版本的 IE (8/7/6) 中工作:

http://jsfiddle.net/lazerblade01/d2QtL/9/

【讨论】:

    【解决方案4】:

    当你生成表格时,给 html 单元格添加一个属性

     <td data-course-name="math">math</td>
    

    使用jquery时试试

     $('td[data-course-name=' + name of the course + ']').css( apply css here)
    

    很简单

    【讨论】:

    • 很遗憾,这需要使用 HTML5,因此如果有人使用 IE 8/7/6 访问此页面,这将不起作用。
    猜你喜欢
    • 1970-01-01
    • 2019-07-02
    • 2018-10-06
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    相关资源
    最近更新 更多