jQuery自动完成,同一字段中有更多项目 [英] jquery autocomplete with more items in the same fields

查看:94
本文介绍了jQuery自动完成,同一字段中有更多项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

进入城镇后,我会使用此功能自动填写国家/地区

I use this function to autocomplete country after enter town

 $(function() {
function log( message ) {
  $( "<div>" ).text( message ).prependTo( "#log" );
  $( "#log" ).scrollTop( 1 );
}

$( "#town2" ).autocomplete({
  source: "<?php echo $absolute_site . "autocomplete/autocompletetown.php" ?>",
  select: function( event, ui ) {
                    var item = ui.item; // probably modification goes here...
                if(item) {
                    $("#country2").val(item.country);                       
                }

  }
})
    .autocomplete( "instance" )._renderItem = function( ul, item ) {
  return $( "<li>" )
    .append( "<a>" +item.value + "," +item.state + "," + item.country + "</a>" )
    .appendTo( ul );
};

});

目标 我如何在#town2字段中获得item.value和item.state,以便在#town2字段中选择城镇后,我将得到类似的信息

GOAL How can I have the item.value and the item.state in the field #town2, so that after selecting the town in the field #town2 I will have something like

迈阿密位于佛罗里达州(迈阿密是item.value,佛罗里达州是item.state). 我确实到处搜索,但是我只能找到多个值(而不是多个项目...)谢谢!

Miami is in Florida (where Miami is item.value and Florida is item.state). I did search everywhere, but I can find only multiple values (and not multiple items...) Thanks!!

推荐答案

应该能够像对国家/地区一样简单地设置值.另外return false可以防止插件本身设置值

Should be able to simply set the value the same way you do for country. Also return false to prevent plugin itself setting the value

 select: function( event, ui ) {
        var item = ui.item; 
        if(item) {
              $("#country2").val(item.country); 
              $(this).val(item.value +' is in ' + item.state);   
              return false;                   
        }    
  }

这篇关于jQuery自动完成,同一字段中有更多项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆