问题
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