问题
I write this Html and use Bootstrap-select to create jquery select
<select id="type" class="selectpicker">
<option>Select...</option>
<option value="1">1</option>
<option value="2">2</option>
how can I check mouseover or hover event with jquery ? I want to alert the value of options.
Bootstrap-select creates select box SelectBox is constructed in the following manner:
<ul class="dropdown-menu inner "><li class="selected active"><a role="option"
aria-disabled="false" tabindex="0" class="selected active" aria-selected="true">
<span class="glyphicon glyphicon-ok check-mark"></span><span
class="text">Select...</span></a></li><li><a role="option" aria-disabled="false"
tabindex="0" aria-selected="false"><span class="glyphicon glyphicon-ok check-
mark"></span><span class="text">1</span></a></li><li><a role="option" aria-
disabled="false" tabindex="0" aria-selected="false"><span class="glyphicon
glyphicon-ok check-mark"></span><span class="text">2</span></a></li></ul>
solved this in the following manner
$('.bootstrap-select').on('mouseover', function (e) {
var $target = $(e.target.children);
if ($target.is('span')) {
console.log($target.text());
}
回答1:
The <select>
gets wrapped in a container with class bootstrap-select
so after you initialize plugin you can do:
$('.bootstrap-select').on('mouseenter', function(e){
console.log($(this).find('select').val())
})
Or you could delegate the listener before initializing plugin
$(document).on('mouseenter','.bootstrap-select', function(e){
console.log($(this).find('select').val())
})
DEMO
来源:https://stackoverflow.com/questions/53706399/get-values-of-bootstrap-select-options-using-jquery