【问题标题】:jquery selection of child elements given status of sibling给定兄弟状态的jquery选择子元素
【发布时间】:2012-08-31 20:51:44
【问题描述】:

给定一个html,例如:

<ul id="timesheetList">                                              
<li>
    <table>
        <tbody>
            <tr>
                <td>
                    <input type="checkbox" class="bigcheck">
                </td>
                <td>
                    <p class="hiddenId">73</p>
                </td>
            </tr>
        </tbody>
    </table>
</li>
<li>
    <table>
        <tbody>
            <tr>
                <td>
                    <input type="checkbox" class="bigcheck">
                </td>
                <td>
                    <p class="hiddenId">44</p>
                </td>
            </tr>
        </tbody>
    </table>
</li>
    <!-- the list goes on... -->

如何为类为“bigcheck”的所有“已检查”(其状态已检查)复选框选择所有 IDS(在本例中为 44 和 73)?

谢谢

【问题讨论】:

  • 我认为在你需要问这个问题之前,你的逻辑可能还有很多更紧迫的问题。页面上没有针对初学者的 ID。只需 p 带有类 hiddenId 的标签。它们完全是两个不同的东西。
  • 是的,我需要这些段落,我可以把它们找出来得到里面的 ID。

标签: javascript jquery jquery-selectors


【解决方案1】:

你可以这样使用:

var tds = $('td p.hiddenId').filter(function() {
    return $(this).parent().prev().children('input.bigcheck:checked').length != 0;
});
// tds contains the list of TDs matching your criteria

【讨论】:

    【解决方案2】:
    1. 首先获取所有选中的复选框。你可以使用

      $("input:checkbox[name=type]:checked").each(function(){

      });

      1. 然后,您需要使用 parentNode() 到达 TD
      2. 然后,使用 nextSibling(),立即到达下一个 TD。
      3. 然后,TD.childNodes[1].innerHTML,会给你结果

    注意:如果你使用这种方法,那么你的结构应该保持不变。

    【讨论】:

      【解决方案3】:

      试试这个:

      var ids = [];
      $("td > input.bigcheck:checked").each(function() {
          ids.push($(this).parent().next("td").children("p").text());
      });
      console.log(ids);
      

      您应该等待可能为您提供完整解决方案的答案,而不是接受只是指导的东西

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-13
        • 2015-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多