私信  •  关注

Lawrence Cherone

Lawrence Cherone 最近创建的主题
Lawrence Cherone 最近回复了
6 年前
回复了 Lawrence Cherone 创建的主题 » 对两个或多个参数使用“if”语句的jQuery

你的循环 .item-name 假设存在多个,但随后选择。项名称再次需要文本值。

相反,循环父元素,然后找到要操作的子元素,例如:

$("table tbody tr").each(function() {
  if ($(this).find(".item-name").text() == 'Chair' && $(this).find(".item-weight").text() >= 20.0) {
    $(this).css("font-weight", "bold")
      .css("background-color", "red");
  } else {
    $(this).css("font-weight", "normal");
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="col-xs-12">
  <table class="table table-striped table-responsive">
    <thead>
      <tr>
        <td colspan="8" class="room-report-heading-td">
          <div class="room-report-heading">
            <div class="each-room  pull-left">
              <span class="room-number">1.</span>
              <span class="room-name">Room</span>
            </div>
            <div class="each-room-statistics pull-right">
              <span class="room-stat">
              1 items
              •
              5.0ft<sup>3</sup>
              •
              20.0lb
            </span>
            </div>
            <br style="clear:both;">
          </div>
        </td>
      </tr>
      <tr class="columns-headings">
        <th class="item-count">Count</th>
        <th class="item-name">Name</th>
        <th class="item-volume">Volume</th>
        <th class="item-total-volume">Total Volume</th>
        <th class="item-weight">Weight</th>
        <th class="item-total-weight">Total weight</th>
      </tr>
    </thead>
    <tbody>

      <tr>
        <td class="item-count"><span>1</span></td>
        <td class="item-name"><span>Chair</span></td>
        <td class="item-volume"><span>5.0</span></td>
        <td class="item-total-volume"><span>5.0</span></td>
        <td class="item-weight"><span>20.0</span></td>
        <td class="item-total-weight"><span>20.0</span></td>
      </tr>

      <tr>
        <td class="item-count"><span>1</span></td>
        <td class="item-name"><span>Bed</span></td>
        <td class="item-volume"><span>5.0</span></td>
        <td class="item-total-volume"><span>5.0</span></td>
        <td class="item-weight"><span>60.0</span></td>
        <td class="item-total-weight"><span>20.0</span></td>
      </tr>
    </tbody>
  </table>

注意,如果页面上有许多表,则需要创造性地查找特定的表,只需知道其dom路径即可。

6 年前
回复了 Lawrence Cherone 创建的主题 » 使用jquery从动态按钮获取ID

不要使用 id 代替使用 data- 属性,然后使用类以按钮为目标,例如:

<button data-id="'.$row['id_ktp'].'" class="valueButton"></button>

然后在jquery中,可以使用数据API获取值。

<script>
$(".valueButton").click(function() {
  alert($(this).data('id'));
});
</script>

我忽略了以下几点:

  • 脚本标记错误 <script>src="jquery-3.3.1.js"</script>
  • 遗失 <tbody>
  • 注意它们的自动“的”,有些编辑只是在你打字的时候加进去的,以保持你的警觉。