jQuery获取select

时间:2019-01-14 09:29:38

标签: javascript jquery bootstrap-multiselect bootstrap-selectpicker

我目前正在进行多重选择(选择选择器),我想获取用户刚刚选择的元素的值。

这就是我所拥有的:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.1/css/bootstrap-select.css" />

<select class="form-control selectpicker" multiple data-live-search="true" id="select_example">
  <option value="1">First</option>
  <option value="2">Second</option>
  <option value="3">Third</option>
</select>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.1/js/bootstrap-select.min.js"></script>
<script>
     $(document).ready(function (){
         $(document).on('changed.bs.select', '#select_example', function(ev, picker) {
             alert($(this).val());
         });
     });
     
</script>

“ $(this).val()”给了我一组选定值。我想要的是,例如,如果已经选择了 1 ,并且用户选择了 2 ,则知道刚刚选择了2。或者,如果选择了 1 2 且用户取消选择了 2 ,则会知道刚刚取消选择了 2

谢谢!

更新:

多亏了Aditya Sharma,我找到了解决问题的方法:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.1/css/bootstrap-select.css" />

<select class="form-control selectpicker" multiple data-live-search="true" id="select_example">
  <option value="1">First</option>
  <option value="2">Second</option>
  <option value="3">Third</option>
</select>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.1/js/bootstrap-select.min.js"></script>
<script>
     $(document).ready(function (){
         $(document).on('changed.bs.select', '#select_example', function(e, clickedIndex, isSelected, previousValue) {
             alert($('#select_example option').eq(clickedIndex).val() + "  " + isSelected);
         });
     });
     
</script>

0 个答案:

没有答案