jQuery combobox onchange event in bootstrap not working

别等时光非礼了梦想. 提交于 2019-12-11 04:01:40

问题


I am using THIS plugin to implement combobox . Also I need to get the value of the select list, so I have added the following code :

$(document).ready(function(){
      $('.combobox').combobox({
          select: function (event, ui) { 
            alert("the select event has fired!"); 
        } 
      });
  });

stackoverflow solution. But in my case no alert is shown ! How can I get the selected option value ?

JSFiddle


回答1:


Checking the plugin homepage, I didn't find any option called select.

You can add a change event to the textfield created and select the value.

 $("input[type='text'].combobox").on("change", function () {
      alert($(this).val());
 });

Demo fiddle




回答2:


That works, but will give you the text, not the value, also if you have the clearIfNoMatch option activated will not give you what you want.

You can try this:

$("div.combobox-container input[type=hidden]").on("change", function () {

      console.log($(this).val());
});



回答3:


I run into the same issue and managed to solve it by listening to change event on select element, not input element.

The thing is that the plugin inserts hidden input and gives it a name that initially was set to select element. I run into issues when listening for change event on hidden input, but the very same thing works for select. So instead of $("input[name='OriginalSelectName']").on('change', ...) make parent container easy to refer to and attach to select $("div[name='SelectDiv'] select").on('change', ...)

<div name="SelectDiv">
    <div class="combobox-container">
        <input type="hidden" name="OriginalSelectName" value="">
        ...
    </div>

    <select class="combobox form-control" style="display: none;">
        <option value="" selected="selected">Nothing</option>
            <option value="e4d4a3bc-e2f7-4494-9ba6-192cff81b10d">Product A</option>
            <option value="d20e202a-041f-4788-9313-3a649f9bb313">Product B</option>
    </select>
</div>


来源:https://stackoverflow.com/questions/26667882/jquery-combobox-onchange-event-in-bootstrap-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!