Get values of Bootstrap-select options using jquery

被刻印的时光 ゝ 提交于 2021-01-29 10:27:36

问题


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

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