使用 jquery 从 bootstrap 选择器文本框获取值

get value from bootstrap selectpicker text box using jquery

如果用户输入了一些文本但没有触发任何内容,我想从 bootstrap select 选择器文本框中获取值

<select id="user_search" class="form-control" data-live-search='true' search='true'>
    <option>option 1</option>
    <option>option 2</option>
    <option>option 3</option>
</select>

<script>
    $("user_search# input").live("keyup", function (e) {
        alert($(this).val());
    });
</script>

确保你已经添加了那些 js 和 CSS 以及我写的 jquery 代码

$(document).ready(function() {
  $(document).on('keyup', '#userDropdown .bs-searchbox input', function(e) {
    alert($(this).val());
  });
  
  $(document).on('keyup', '#cityDropdown .bs-searchbox input', function(e) {
    alert($(this).val());
  });
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.1/css/bootstrap-select.css" />

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.1/js/bootstrap-select.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div id="userDropdown">
  <select name="user_search" data-live-search="true" class="selectpicker">
    <option>option 1</option>
    <option>option 2</option>
    <option>option 3</option>
  </select>
</div>

<div id="cityDropdown">
  <select name="city_search" data-live-search="true" class="selectpicker">
    <option>City 1</option>
    <option>City 2</option>
    <option>City 3</option>
  </select>
</div>