【问题标题】:JqueryUI Autocomplete prevent call on every keypressJqueryUI自动完成防止每次按键调用
【发布时间】:2017-08-30 22:02:42
【问题描述】:

我正在使用 JqueryUI 自动完成功能,我想做的是防止 JqueryUI 自动完成功能在每次按键时都进行调用。一切都以完美的方式工作。我已将文本字段与自动完成绑定,但每次按键时都会拨打电话。如何防止调用特定键?例如,我想阻止对箭头键的调用。这是我在 Coffee 中的代码。

$("#location-search").autocomplete
        source: (request, response) ->
          if request.term.length < 1
            $(".pen-dropdown").hide() 
          else
            $.ajax
              url: "http://ws.geonames.org/searchJSON?country=US&lang=en&username=awais545"
              dataType: "jsonp"
              data:
                maxRows: 10
                name_startsWith: request.term

              success: (data) ->
                rows = new Array()
                data = data.geonames
                i = 0

                while i < data.length
                  rows[i] =
                  value:        data[i].name
                  country_code: data[i].adminCode1
                  i++
                rows

                $("#location-search").parent().find(".pen-dropdown ul").html("")

                if $("#location-search").parent().find(".header-loaction").length > 0
                  $("#location-search").parent().find(".pen-dropdown ul").append("<li><a href='#'> All Area </a></li>")

                for row, i in rows
                  $("#location-search").parent().find(".pen-dropdown ul").append("<li><a href='#'> #{row['value']}, #{row['country_code']} </a></li>")
                  window.dropdownli = $(".pen-dropdown li")
                  window.dropdownliSelected = undefined

                for li in $("#location-search").parent().find(".pen-dropdown ul li")
                  $(li).click(setLocationTitle)

                $("#location-search").parent().find(".pen-dropdown").show()

谢谢

【问题讨论】:

  • 但我在任何地方都没有事件对象,我怎样才能让按键与自动完成绑定?
  • 我不熟悉Coffeescript,但在你的自动完成功能中你应该能够绑定stopPropagation()处理程序,就像这样:$("#location-search").bind('autocompletechange', function(event, ui) { event.stopPropagation(); });

标签: jquery jquery-ui autocomplete


【解决方案1】:

我有类似的问题。我在自动完成中使用事件“search(event, ui)”修复了它。

 $("#autocompleteText").autocomplete({
            search: function (event, ui) {
           /*keyCode will "undefined" if user presses any function keys*/
                if(event.keyCode)
                { event.preventDefault(); }
            },
            autoFocus:false,
            minLength:3,
            source: function (request, response) {
                //make remote call for result.                   
            }

        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-31
    • 2014-04-30
    相关资源
    最近更新 更多