【问题标题】:Adding/removing class to/from table cells that is triggered by child element event向/从由子元素事件触发的表格单元格添加/删除类
【发布时间】:2013-11-19 20:09:27
【问题描述】:

我正在使用 jQuery 1.4。每次选中 "<td>" 内的单选按钮时,我都会尝试突出显示表的 <td> 标记,如果未选中,则删除突出显示类。

这是标记:

CSS: .highlight-blue { 背景色:#81BEF7; }

<table id="tblContainer">

        <tr>
            <td>
                <input type="radio" name="book" value="book1" /> Book 1</td>
            <td>
                <input type="radio" name="book" value="book2" /> Book 2</td>
            <td>
                <input type="radio" name="book" value="book3" /> Book 3</td>
                            <td>
                <input type="radio" name="book" value="book4" /> Book 4</td>
     </tr>
</table>

Javascript:

// highlight checked radion button
            $('#tblContainer :radio').click(function() {

            //add class to hilghlight selected radio button
            var cell = $(this).closest('td');
                if ($(this).is(':checked')) {
                    cell.addClass('highlight-blue');
                }
                else {
                //remove highlight class
                    cell.removeClass('highlight-blue');
                }

            });

问题在于它不会从先前选择的单选按钮中删除该类。

更新 1: 在此处查看新的更新标记http://jsbin.com/egusud/7

【问题讨论】:

    标签: javascript jquery jquery-selectors


    【解决方案1】:

    是的,您会想要上升到行级别(如果单选按钮只是在行中彼此相关)或表级别(如果它们跨行彼此相关)。

    假设仅在行内 (live example):

    $('#tblContainer :radio').click(function() {
        var $this = $(this);
    
        // Remove highlight
        $this.closest("tr").find("td.highlight-blue").removeClass("highlight-blue");
    
        // Add it to this one
        $this.closest("td").addClass("highlight-blue");
    });
    

    请注意,如果单选按钮被选中,您只会点击它,因此无需检查。


    题外话:如果您添加labels (live copy),人们会更容易点击这些单选按钮:

    <label><input type="radio" name="book" value="book1"> Book 1</label>
    

    如果是这样,您可能希望将此 CSS 样式添加到标签 (live copy):

    label {
        cursor: pointer;
    }
    

    ...使它们更容易被点击。


    更新:从 cmets 看来,您的单选按钮彼此位于不同的行中,并且您还混有其他单选按钮,如下所示:

    <table id="tblContainer">
      <thead>
        <tr>
          <th>Books</th>
          <th>Magazines</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>
            <label><input type="radio" name="book" value="book1"> Book 1</label>
          </td>
          <td>
            <label><input type="radio" name="magazine" value="magazine1"> Magazine 1</label>
          </td>
        </tr>
        <tr>
          <td>
            <label><input type="radio" name="book" value="book2"> Book 2</label>
          </td>
          <td>
            <label><input type="radio" name="magazine" value="magazine2"> Magazine 2</label>
          </td>
        </tr>
        <tr>
          <td>
            <label><input type="radio" name="book" value="book3"> Book 3</label>
          </td>
          <td>
            <label><input type="radio" name="magazine" value="magazine3"> Magazine 3</label>
          </td>
        </tr>
      </tbody>
    </table> 
    

    在这种情况下,要从旧的类中删除该类,您只需找到具有相同名称的其他单选按钮,然后找到具有该类的父单元格 (live copy):

    jQuery(function($) {
    
      $('#tblContainer :radio').click(function() {
        var $this = $(this);
    
        // Remove highlight from cells containing
        // radio buttons with this same name
        $this
          .closest("table")
          .find('input:radio[name="' + this.name + '"]')
          .closest("td.highlight-blue")
          .removeClass("highlight-blue");
    
        // Add it to this one
        $this.closest("td").addClass("highlight-blue");
      });
    
    });
    

    或者,“标记类”方法(这里我使用了“rb-”加上单选按钮的名称,但您需要确保单选按钮名称对类名有效)(live copy ):

    jQuery(function($) {
    
      $('#tblContainer :radio').click(function() {
        var $this = $(this);
    
        // Remove previous highlight if any
        $("td.highlight-blue.rb-" + this.name).removeClass("highlight-blue");
    
        // Add it to this one
        $this.closest("td").addClass("highlight-blue rb-" + this.name);
      });
    
    });
    

    【讨论】:

    • 谢谢你。它回答了我在这里提出的问题。但我在这里更新了标记jsbin.com/egusud/7 请看一下。不过,我无法从 标记中删除“highlight-blue”类。
    • @Narazana:再看一下答案,该链接仍然与原始链接具有相同的基本问题。当单选按钮关闭时,您不会得到click,因此您无法通过单击删除该按钮的类。再看一下上面的代码:当单击单选按钮时,我们从 other 单选按钮中删除该类。您需要更改上面的代码,因为您的按钮是不同的行(改为上表),但基本上是相同的。
    • 我试图先找到父“tr”,然后找到像这样 $this.parent("tr").find("td.highlight- blue").removeClass("highlight-blue");但仍然没有运气。 jsbin.com/egusud/8
    • @Narazana:再说一遍:您需要更改上面的代码,因为您的按钮位于不同的行中(改为上表)。
    • 如果我走到“桌子” $this.closest("table").find("td.highlight-blue").removeClass("highlight-blue");它也会影响其他标签。除了同一张表中的单选按钮之外,我还有其他标签,这就是为什么我想去父母“tr”并找到具有“highlight-blue”的“td”孩子。我尝试了最接近(“tr”)和父(“tr”),它不会从“td”中删除“highlight-blue”类。
    【解决方案2】:
    $("#tbl_ClientSites > tbody > tr").live("click", function() {
            var trInstance = $('#tbl_ClientSites > tbody').find('tr');                   
                    trInstance.removeClass('highlighted');
                    trInstance.find('tr:eq(0)').removeClass('highlighted');
                    var instance = $(this);
                    instance.addClass('highlighted');
                    instance.find('tr:eq(0)').addClass('highlighted');      
        });
    

    删除先前选择的突出显示的行。当前点击的行高亮显示。

    【讨论】:

      【解决方案3】:
      $('#tblContainer :radio').click(function() {
      //remove highlight from all
      $('#tblContainer td').removeClass('highlight-blue')
                  //add class to hilghlight selected radio button
                  var cell = $(this).closest('td');
      
      
                          cell.toggleClass('highlight-blue');
      
      
                  });
      

      【讨论】:

        【解决方案4】:

        如果你想从之前选择的单选按钮中删除类,你必须遍历所有单选按钮并删除类,除了 $(this)。或者您可以使用 ID 代替 class,然后您只需从先前选择的 ID 中删除 ID,并将其添加到新 ID。

        【讨论】:

        • 感谢您回答我的问题。我投票给你。
        猜你喜欢
        相关资源
        最近更新 更多
        热门标签