【问题标题】:get jquery autocomplete selected value on blur event在模糊事件上获取 jquery 自动完成选择的值
【发布时间】:2014-05-09 16:23:57
【问题描述】:

我的文本框具有自动完成功能,我已将 autoFocus 属性设置为 true

$( "#stateList" ).autocomplete({ autoFocus: true });

在提供上述属性后,我的 stateList 自动完成框会在用户按 Enter 键或 Tab 键时选择并填充第一个值。

我一直在尝试实现相同的功能,当“stateList”文本框出现模糊事件时,会自动选择最先列出的值。

我的代码在这里,没有调用模糊事件 http://jsfiddle.net/c6sWw/168/

【问题讨论】:

    标签: javascript jquery html jquery-ui autocomplete


    【解决方案1】:

    试试这个:

    $("#stateList").bind('autocompletechange', function(event, ui) {
        alert(ui.item.value);
    });
    

    DEMO

    【讨论】:

      【解决方案2】:
      $( "#stateList" ).autocomplete({
          autoFocus:true,
          source: states,
          open: function( event, ui ) {
              var firstElement = $(this).data("autocomplete").menu.element[0].children[0]
                 , inpt = $('#stateList')
                 
                 , firstElementText = $(firstElement).text();
                  inpt.val(firstElementText)
             
        
          }
       
      })
      

      已解决在 jsfidle http://jsfiddle.net/c6sWw/170/

      参考jquery autocomplete auto fill field with 1st value and highligh added part

      【讨论】:

      • 这不起作用,当我将这段代码放入 jsp 时,菜单未定义
      猜你喜欢
      • 2014-05-09
      • 1970-01-01
      • 2012-09-25
      • 1970-01-01
      • 2011-12-05
      • 2014-11-19
      • 1970-01-01
      • 2017-02-16
      • 1970-01-01
      相关资源
      最近更新 更多