如何在“更改时”事件中的多选中获取当前选择的选项?

时间:2019-04-17 13:55:28

标签: jquery jquery-plugins bootstrap-select bootstrap-selectpicker

如何在引导选择多重选择中获取当前选择的选项。我能够获得最后选择的选项,但是如果取消选择,它就无法正常工作,所以我需要当前选项

$('#beaconId').change(function(e) {
  e.preventDefault();
  var value = $("option:selected:last", this).val();
  alert(value);
  if ($("#beaconId option[value='" +
      $("option:selected:last", this).val() + "']").prop('selected') == true) {
    alert('checked');
    var n = Math.floor((Math.random() * 2));
    alert(n);
    if (!n) {
      alert('location not found');
      $("#beaconId option[value='" + $("option:selected:last", this).val() + "']").prop('selected', false);
      $('#beaconId').selectpicker('refresh');
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="select-picker-wrap">
  <select name="beacon_id[]" class="selectpicker" title="Select Beacon" id="beaconId" multiple>

    <option value="84d5" class="checkBeacon">blueberry Test</option>

    <option value="6a02" class="checkBeacon">Test Beacon</option>

    <option value="85e5" class="checkBeacon">coconut</option>

  </select>

</div>

0 个答案:

没有答案