当使用'最接近的':jQuery

Select2 .val function returning empty value when selected using 'closest' : jQuery

本文关键字:jQuery 最接近      更新时间:2023-09-26

我有一个包含复选框、一些文本和select2下拉列表的表。用户将选中一个复选框,然后在select2下拉框中选择一个值。我试图了解用户是否在select2框中选择了与选中复选框相对应的值。

以下是我的代码:

var value = [{
  id: 0,
  text: 'enhancement'
}, {
  id: 1,
  text: 'bug'
}, {
  id: 2,
  text: 'duplicate'
}, {
  id: 3,
  text: 'invalid'
}, {
  id: 4,
  text: 'wontfix'
}]
$(document).ready(function() {
  $(".bulk_relationship_dropdown").select2({
    data: value
  })
  $("#submit").click(function() {
    jQuery(".bulk_relationship_dropdown").each(function() {
      if (jQuery(this).val() != "") {
        console.log(jQuery(this).val());
      }
    })
    $(".cb:checked").each(function() {
      var cb = $(this);
      if (cb.closest('tr').find('.bulk_relationship_dropdown').val() == "") {
        console.log("here"); //do something
      }
    })
  });
});
.select2-container {
  margin-top: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.min.css" rel="stylesheet" />
<br>
<table>
  <tr>
    <td>
      <input type="checkbox" class="cb">
    </td>
    <td>some random text</td>
    <td>
      <input type="text" style="width: 120px" class="bulk_relationship_dropdown">
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" class="cb">
    </td>
    <td>some random text</td>
    <td>
      <input type="text" style="width: 120px" class="bulk_relationship_dropdown">
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" class="cb">
    </td>
    <td>some random text</td>
    <td>
      <input type="text" style="width: 120px" class="bulk_relationship_dropdown">
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" class="cb">
    </td>
    <td>some random text</td>
    <td>
      <input type="text" style="width: 120px" class="bulk_relationship_dropdown">
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" class="cb">
    </td>
    <td>some random text</td>
    <td>
      <input type="text" style="width: 120px" class="bulk_relationship_dropdown">
    </td>
  </tr>
</table>
<button id="submit">submit</button>

在上面的代码中,当我遍历所有复选框时,.val()函数会给我正确的值。这就是这部分。

jQuery(".bulk_relationship_dropdown").each(function() {
          if (jQuery(this).val() != "") {
            console.log(jQuery(this).val());
          }
        })

但是,当我找到与下面这样的复选框相对应的select2框时,.val()函数总是返回空字符串。

$(".cb:checked").each(function() {
          var cb = $(this);
          if (cb.closest('tr').find('.bulk_relationship_dropdown').val() == "") {
            console.log("here"); //do something
          }
        })

我做错了什么?

我正在使用select2 3.5.2

select2将input上的类复制为它生成的非表单控件结构的一部分。生成的结构(当前为span)将是find返回的jQuery集合中的第一个结构,而val只尝试读取集合中的第1个元素,因此将返回undefined。只需将input添加到选择器中,就可以找到输入(select2隐藏但保持最新):

cb.closest("tr").find("input.bulk_relationship_dropdown")
// --------------------^^^^^

更新片段:

var value = [{
  id: 0,
  text: 'enhancement'
}, {
  id: 1,
  text: 'bug'
}, {
  id: 2,
  text: 'duplicate'
}, {
  id: 3,
  text: 'invalid'
}, {
  id: 4,
  text: 'wontfix'
}]
$(document).ready(function() {
  $(".bulk_relationship_dropdown").select2({
    data: value
  })
  $("#submit").click(function() {
    jQuery("input.bulk_relationship_dropdown").each(function() {
      if (jQuery(this).val() != "") {
        console.log(jQuery(this).val());
      }
    })
    $(".cb:checked").each(function() {
      var cb = $(this);
      if (cb.closest('tr').find('input.bulk_relationship_dropdown').val() == "") {
        console.log("here"); //do something
      }
    })
  });
});
.select2-container {
  margin-top: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.min.css" rel="stylesheet" />
<br>
<table>
  <tr>
    <td>
      <input type="checkbox" class="cb">
    </td>
    <td>some random text</td>
    <td>
      <input type="text" style="width: 120px" class="bulk_relationship_dropdown">
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" class="cb">
    </td>
    <td>some random text</td>
    <td>
      <input type="text" style="width: 120px" class="bulk_relationship_dropdown">
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" class="cb">
    </td>
    <td>some random text</td>
    <td>
      <input type="text" style="width: 120px" class="bulk_relationship_dropdown">
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" class="cb">
    </td>
    <td>some random text</td>
    <td>
      <input type="text" style="width: 120px" class="bulk_relationship_dropdown">
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" class="cb">
    </td>
    <td>some random text</td>
    <td>
      <input type="text" style="width: 120px" class="bulk_relationship_dropdown">
    </td>
  </tr>
</table>
<button id="submit">submit</button>