【问题标题】:Autocomplete Jquery with user restriction具有用户限制的自动完成 Jquery
【发布时间】:2012-02-27 05:10:34
【问题描述】:

我正在研究自动完成并使用了这个http://jqueryui.com/demos/autocomplete/#combobox

现在我的查询是这个 api 让用户可以灵活地输入和搜索组合框的值,但由于这个用户可以输入任何内容。但是有没有办法限制用户提交错误的搜索查询。这是因为我的组合框将用作搜索条件,并且它的值被提交到下一页,并且由于此功能,它向页面提交了错误的数据。

提前谢谢你... 谢谢

【问题讨论】:

    标签: jquery jquery-ui


    【解决方案1】:

    您可以尝试自定义匹配器功能来强制匹配或清除字段...

    $("#input").autocomplete({ << initialise the autocomplete here >>})
    .on('blur', function(event){
      // Grab the autocomplete object
      var autocomplete = $(this).data("autocomplete");
      var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val()) + "$", "i");
      // Iterate through the autocomplete list items to find any partial or full matches
      autocomplete.widget().children(".ui-menu-item").each(function() {
        var item = $(this).data("item.autocomplete");
        if (matcher.test(item.value || item)) {
          //There was a match
          matchcount++;
          autocomplete.selectedItem = item;
          return;
        }
      });
      if (autocomplete.selectedItem) {
        //if there was a match trigger the select event on that match
        autocomplete._trigger("select", event, {
          item: autocomplete.selectedItem
        });
      //there was no match, clear the input
      } else {
          $(this).val('');
      }
    });
    

    【讨论】:

    • 你能在 JSfiddle 上实现吗..我没有得到 $("#input").autocomplete({ >}) 在这里初始化什么?
    • 也许这是一个更清晰/更容易的答案:stackoverflow.com/questions/2909077/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 2011-05-03
    • 2021-06-11
    • 2017-08-28
    • 2011-11-28
    相关资源
    最近更新 更多