Bootstrap-Select和键盘导航

时间:2016-07-26 21:29:48

标签: jquery twitter-bootstrap bootstrap-select

我正在使用bootstrap-select https://silviomoreto.github.io/bootstrap-select/

我有以下下拉列表,其中包含图片网址作为值。

当我使用鼠标并选择每个选项时,一切正常。但是,当我使用键盘的向上和向下箭头并突出显示每个选项时,我希望触发一个事件,以便我可以翻转可用的图像预览。 (此处未显示)当我使用鼠标单击突出显示的选项时,它会显示按预期选择的图像的预览。

问题是如何实现它,以便当用户使用键盘箭头上下移动时,选择该值,这样他们就可以轻松翻转想象可用图像而无需每次都输入。我试过以下

$('.selectpicker').on('changed.bs.select', function (e, clickedIndex, newValue, oldValue) {
    var selected = $(e.currentTarget).val();
    alert(selected);
});
 but this only works after I hit enter. Any suggestions. 


    <select class="selectpicker form-control" data-size="10" name="cboImage" id="cboImage" onchange="cboImage_onChange()" size="1" tabindex="-98">

    <option value="images/1x1.gif">[BLANK IMAGE]</option>
    <option value="upload/33/img_3009.jpg">10-16-2015 11-56-30 AM.jpg</option>
    <option value="upload/00/img_3031.jpg">10478631_1494051514186448_2207392604969581462_o.jpg</option>
    <option value="upload/BB/img_3003.jpg" selected="">10920935_1385769295058553_7186298008992739460_n.jpg</option>
    <option value="upload/22/img_3006.jpg">11-29-2014 6-35-20 PM.jpg</option>
    <option value="upload/22/img_3004.jpg">11025665_1380482578935990_5955532852167626303_n.jpg</option>
    <option value="upload/77/img_2913.jpg">12339668_1517870915178926_9132688390289736139_o.jpg</option>
    <option value="upload/EE/img_2972.jpg">269586_10151304206069257_1562006627_n.png</option>
    <option value="upload/88/img_3005.jpg">3305476.jpg</option>

<option value="upload/00/img_3008.jpg">536962_1.jpg</option>
<option value="upload/FF/img_2969.jpg">9-3-2015 9-03-19 AM.jpg</option>
<option value="upload/77/img_2971.jpg">analyze_image.jpg</option>
<option value="upload/AA/img_3007.jpg">Copy of FL-SAR00_226876_1.jpg</option>
<option value="upload/11/img_2914.jpg">holiday2015_1_image.jpg</option>
<option value="upload/33/img_3015.jpg">IMAG0002.jpg</option>
<option value="upload/BB/img_3013.jpg">IMAG0002.jpg</option>
<option value="upload/55/img_3014.jpg">IMAG0003.jpg</option>
<option value="upload/BB/img_3016.jpg">IMAG0003.jpg</option>
<option value="upload/66/img_3017.jpg">IMAG0004.jpg</option>
<option value="upload/CC/img_3012.jpg">IMAG0004.jpg</option>
<option value="upload/66/img_3019.jpg">IMAG0023.jpg</option>
<option value="upload/FF/img_3018.jpg">IMAG0053.jpg</option>
<option value="upload/88/img_3011.jpg">image119037-180ae8ff18269a6e395200cb831d928e.jpg</option>
<option value="upload/66/img_3029.jpg">IMG_1113.JPG</option>
<option value="upload/CC/img_3028.jpg">IMG_1114.JPG</option>
<option value="upload/FF/img_3027.jpg">IMG_1123.JPG</option>
<option value="upload/BB/img_3030.jpg">IMG_1125.JPG</option>
<option value="upload/AA/img_2917.jpg">L7ViI0Pm_400x400.png</option>
<option value="upload/CC/img_3025.jpg">MuscleMadison11yrs.jpg</option>
<option value="upload/FF/img_2960.jpg">My Image</option>
<option value="upload/BB/img_3026.jpg">TimesSquare2013.jpg</option>
<option value="upload/99/img_3010.jpg">Windsor-at-Lakoya-Front-Elevation-1024x656.jpg</option>

            </select>

1 个答案:

答案 0 :(得分:1)

为了使用UP / DOWN键选择你需要在$(&#39; div.dropdown-menu.open li&#39;)的keydown事件上附加处理程序的元素。

选择您需要使用的元素:

$('.selectpicker').selectpicker('val', selVal);

所以代码片段是:

&#13;
&#13;
$(function () {
  $('.selectpicker').selectpicker({
    style: 'btn-info',
    size: 4
  });
  $('div.dropdown-menu.open li').on('keydown', function (e) {
    if (e.keyCode == 38) { // Up
      var previousEle = $(this).prev();
      if (previousEle.length == 0) {
        previousEle = $(this).nextAll().last();
      }
      var selVal = $('.selectpicker option').filter(function () {
        return $(this).text() == previousEle.text();
      }).val();
      $('.selectpicker').selectpicker('val', selVal);

      return;
    }
    if (e.keyCode == 40) { // Down
      var nextEle = $(this).next();
      if (nextEle.length == 0) {
        nextEle = $(this).prevAll().last();
      }
      var selVal = $('.selectpicker option').filter(function () {
        return $(this).text() == nextEle.text();
      }).val();
      $('.selectpicker').selectpicker('val', selVal);

      return;
    }
  });
});
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/css/bootstrap-select.min.css">
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/js/bootstrap-select.min.js"></script>

<select class="selectpicker form-control" data-size="10" name="cboImage" id="cboImage" size="1" tabindex="-98">
    <option value="images/1x1.gif">[BLANK IMAGE]</option>
    <option value="upload/33/img_3009.jpg">10-16-2015 11-56-30 AM.jpg</option>
    <option value="upload/00/img_3031.jpg">10478631_1494051514186448_2207392604969581462_o.jpg</option>
    <option value="upload/BB/img_3003.jpg" selected="">10920935_1385769295058553_7186298008992739460_n.jpg</option>
    <option value="upload/22/img_3006.jpg">11-29-2014 6-35-20 PM.jpg</option>
    <option value="upload/22/img_3004.jpg">11025665_1380482578935990_5955532852167626303_n.jpg</option>
    <option value="upload/77/img_2913.jpg">12339668_1517870915178926_9132688390289736139_o.jpg</option>
    <option value="upload/EE/img_2972.jpg">269586_10151304206069257_1562006627_n.png</option>
    <option value="upload/88/img_3005.jpg">3305476.jpg</option>
    <option value="upload/00/img_3008.jpg">536962_1.jpg</option>
    <option value="upload/FF/img_2969.jpg">9-3-2015 9-03-19 AM.jpg</option>
    <option value="upload/77/img_2971.jpg">analyze_image.jpg</option>
    <option value="upload/AA/img_3007.jpg">Copy of FL-SAR00_226876_1.jpg</option>
    <option value="upload/11/img_2914.jpg">holiday2015_1_image.jpg</option>
    <option value="upload/33/img_3015.jpg">IMAG0002.jpg</option>
    <option value="upload/BB/img_3013.jpg">IMAG0002.jpg</option>
    <option value="upload/55/img_3014.jpg">IMAG0003.jpg</option>
    <option value="upload/BB/img_3016.jpg">IMAG0003.jpg</option>
    <option value="upload/66/img_3017.jpg">IMAG0004.jpg</option>
    <option value="upload/CC/img_3012.jpg">IMAG0004.jpg</option>
    <option value="upload/66/img_3019.jpg">IMAG0023.jpg</option>
    <option value="upload/FF/img_3018.jpg">IMAG0053.jpg</option>
    <option value="upload/88/img_3011.jpg">image119037-180ae8ff18269a6e395200cb831d928e.jpg</option>
    <option value="upload/66/img_3029.jpg">IMG_1113.JPG</option>
    <option value="upload/CC/img_3028.jpg">IMG_1114.JPG</option>
    <option value="upload/FF/img_3027.jpg">IMG_1123.JPG</option>
    <option value="upload/BB/img_3030.jpg">IMG_1125.JPG</option>
    <option value="upload/AA/img_2917.jpg">L7ViI0Pm_400x400.png</option>
    <option value="upload/CC/img_3025.jpg">MuscleMadison11yrs.jpg</option>
    <option value="upload/FF/img_2960.jpg">My Image</option>
    <option value="upload/BB/img_3026.jpg">TimesSquare2013.jpg</option>
    <option value="upload/99/img_3010.jpg">Windsor-at-Lakoya-Front-Elevation-1024x656.jpg</option>
</select>
&#13;
&#13;
&#13;