当使用'nearest'选择时,Select2 .val函数返回空值:jQuery

时间:2016-05-05 10:02:48

标签: javascript jquery html select2

我有一个包含复选框,一些文本和一个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

1 个答案:

答案 0 :(得分:1)

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

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

更新了代码段:

&#13;
&#13;
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
      }
    })

  });


});
&#13;
.select2-container {
  margin-top: 5px;
}
&#13;
<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>
&#13;
&#13;
&#13;