【问题标题】:jQuery selector for second column in HTML table rowHTML表格行中第二列的jQuery选择器
【发布时间】:2015-05-03 21:57:23
【问题描述】:

我的代码的目的是将所选 HTML 表格行中的数据添加到 JavaScript 数组中,如果未选择该行,则将其删除。

https://jsfiddle.net/nicktry/h1636ram/

我现在有两个问题:

  1. 第一次点击时未应用 CSS 样式
  2. 我无法正确使用 jQuery 选择器将表数据添加/删除到数组中。

    $('table tbody tr').click(function(event) {
                if (event.target.type !== 'checkbox') {
                    $(':checkbox', this).trigger('click');
                }
                $("input[type='checkbox']").change(function(e) {
                    if($(this).is(":checked")){
                        $(this).closest('tr').addClass("selected_row");
                        addJob(document.getElementById('jobs'), $(this).closest('tr td:nth-child(2)').text());  
                    }else{
                        $(this).closest('tr').removeClass("selected_row");
                        removeJob(document.getElementById('jobs'), $(this).closest('tr td:nth-child(2)').text());
                    }
                });
            });
    

stackoverflow 社区的智慧将不胜感激!

【问题讨论】:

    标签: javascript jquery html css jquery-selectors


    【解决方案1】:

    你不应该在彼此内部有两个事件。

    移动一下

     $("input[type='checkbox']").not('#check_all').change(function (e) {
    
     });
    

    外面

     $('table tbody tr').click(function(event) { 
    
     });
    

    改变这个

    $(this).closest('tr td:nth-child(2)').text()
    

    到这里

     $(this).parent().next().text()
    

    演示:https://jsfiddle.net/h1636ram/9/

    【讨论】:

    • 谢谢。我稍微调整了您的代码,它现在完全按照我的意愿工作。 jsfiddle.net/nicktry/h1636ram/10
    • @NickT 如果有帮助不要忘记标记我的答案:)
    猜你喜欢
    • 2011-12-28
    • 1970-01-01
    • 2016-11-09
    • 2011-10-02
    • 2011-01-10
    • 2015-02-06
    • 2018-10-11
    • 2019-09-16
    • 1970-01-01
    相关资源
    最近更新 更多