如何在jQuery UI自动完成中覆盖select的默认行为?

前端之家收集整理的这篇文章主要介绍了如何在jQuery UI自动完成中覆盖select的默认行为?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下内容
$('#<%=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,')[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相关文章