dptdb84606 2015-02-25 22:21 采纳率: 0%
浏览 63
已采纳

jQuery UI自动完成与不同的输入

I want to add different autocompletes for different inputs. For example I have the following two input fields:

<input type="text" id="firstname" name="firstname" placeholder="Max" required />
<input type="text" id="lastname" name="lastname" placeholder="Mustermann" required />

So I currently add different autocompletes as follows:

$( document ).ready(function() {
    $( '#firstname' ).autocomplete({
        source: 'autocomplete_firstname.php',
        minLength: 1
    });
    $( '#lastname' ).autocomplete({
        source: 'autocomplete_lastname.php',
        minLength: 1
    });
});

That works fine for me, but maybe is there a better way like a parameter? So that I can use only one class on autocomplete fields and only one .php-file which return the same result?

  • 写回答

2条回答 默认 最新

  • dsg24156 2015-02-25 22:34
    关注

    try this:

    $( document ).ready(function() {
    var focused_inp;
    
        $( '#firstname,#lastname' ).on('focus',function(){ focused_inp = $(this).prop('name');}).autocomplete({
            source: function(req, res){
          $.ajax({
          url: "autocomplete.php",
          dataType: "jsonp",
          data: {action: focused_inp, data: request.term  },
          success: function(result) { res(result)  }
        }) },
            minLength: 1,
    
        });
    
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试