jquery - 如何覆盖 select : within jQuery UI Autocomplete? 的默认行为

标签 jquery jquery-ui autocomplete jquery-ui-autocomplete

我有以下内容:

$('#<%=txtCity.ClientID%>').autocomplete({
  source: function (request, response) {
    var parameters = {
      isoalpha2: '<%=Session["BusinessCountry"].ToString()%>',
      prefixText: request.term
    };
    $.ajax({
      url: '<%=ResolveUrl("~/AtomicService/Assets.asmx/GetCitiesWithState")%>',
      type: 'POST',
      dataType: 'json',
      contentType: 'application/json; charset=utf-8',
      data: JSON.stringify(parameters),
      success: function (data) {
        response($.each(data.d, function (index, value) {
          return {
            label: value.slice(value.indexOf(',')),
            value: parseInt(value.split(',')[0])
          }
        }));
      }

    });
  },
  minLength: 2,
  delay: 50,
  select: function (event, ui) {
    var city = ui.item.label.split(',')[0];
    var state = ui.item.label.split(',')[1];
    alert(city);
    alert(state);
    $('#<%=txtCity.ClientID%>').val(city);
    $('#<%=txtState.ClientID%>').val(state);
  },
});

这都是快乐的日子,除了当我从自动完成列表中选择一个项目时,我不希望自动完成填充 $('#<%=txtCity.ClientID%>')元素。我该怎么做?我看到.insertAfter ,这是我应该看的东西吗?

感谢帮助。

最佳答案

尝试从 select event 返回 false :

...
select: function(event, ui) {
    var city = ui.item.label.split(',')[0];
    var state = ui.item.label.split(',')[1];
    alert(city);
    alert(state);
    $('#<%=txtCity.ClientID%>').val(city);
    $('#<%=txtState.ClientID%>').val(state);
    return false;
},

来自文档(选择事件):

Triggered when an item is selected from the menu; ui.item refers to the selected item. The default action of select is to replace the text field's value with the value of the selected item. Canceling this event prevents the value from being updated, but does not prevent the menu from closing.

You can try it here.

关于jquery - 如何覆盖 select : within jQuery UI Autocomplete? 的默认行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5688036/

相关文章:

jQuery 未定义(在 jQuery UI 中)

javascript - jQuery - 在 div 之间拖放,同时能够调整元素大小

css - 如何在 chrome 自动完成上删除蓝色背景

c++ - Emacs 自动完成功能无法正常工作

c# - 如何向 VS2013 语法扩展 (MEF) 动态添加补全词

jquery - 当 div 滚动到 | 时触发函数jQuery

JQuery.Color 解决方法

jquery - 如何将 jQuery UI 日期选择器附加到动态插入的表单域?

javascript - Angular 从 json 中删除项目

javascript - 如何检查验证文本不是电子邮件?