简体   繁体   English

如何将属性传递给 jquery 自动完成

[英]How to pass attribute to jquery autocomplete

I am having a serious problem with trying to use a jquery autocomplete and javascript is not my strongest skill.我在尝试使用 jquery 自动完成功能时遇到了严重的问题,而 javascript 并不是我最擅长的技能。

I am using the jquery.auto-complete plugin found here: https://github.com/Pixabay/jQuery-autoComplete which is a rewrite of the devbridge.com version.我正在使用在此处找到的 jquery.auto-complete 插件: https : //github.com/Pixabay/jQuery-autoComplete ,它是 devbridge.com 版本的重写。

so far, getting it to work has been no problem, but now i am stuck with a problem and really need some help.到目前为止,让它工作没有问题,但现在我遇到了一个问题,真的需要一些帮助。

i have a form for data entry for a very large database.我有一个非常大的数据库的数据输入表单。 nothing special, but a very large database with lots of fields and about 80% of the data is simple text fields that have very similar or duplicated values, but still varied enough that nothing really other than an autocomplete will make life easier.没什么特别的,但是一个非常大的数据库有很多字段和大约 80% 的数据是简单的文本字段,它们具有非常相似或重复的值,但仍然有足够的变化,除了自动完成之外,没有什么能让生活更轻松。 this can be very time consuming and tedious, especially when database is 1M+ records.这可能非常耗时且乏味,尤其是当数据库有 100 万条记录时。

so since i have around 40 fields that require lookups, this is my current code:所以因为我有大约 40 个需要查找的字段,这是我当前的代码:

$(window).on('load', function () {
    var xhr;
    $('[data-autocomplete]').autoComplete({
        source: function(term, response){
            try { xhr.abort(); } catch(e){}
            xhr = $.get( '/api.php', 
                        { field: $(this).attr('data-autocomplete'),
                          search: term,
                         }, function(data){ response(data); });
        }
    });

...

and this is my field:这是我的领域:

<input type="text" name="data[title]" id="data[title]" data-autocomplete="title" /> 

but for some reason, i can't get the value of the attribute "data-autocomplete" to be passed to the autocomplete function.但由于某种原因,我无法获得要传递给自动完成功能的属性“数据自动完成”的值。 it just comes up as undefined and that is critical for the search它只是未定义,这对搜索至关重要

what i need is a way that i can bind the autocomplete on page load to any text input with an attribute of "data-autocomplete" (so far so good) and then pass that value to the autocomplete thus creating an url of:我需要的是一种方法,我可以将页面加载时的自动完成绑定到具有“数据自动完成”属性的任何文本输入(到目前为止很好),然后将该值传递给自动完成,从而创建以下网址:

api.php?field=[data-autocomplete]&search=[term]

sounds simple, but seems to be exceedingly difficult.听起来很简单,但似乎非常困难。 my other option is to duplicate the autocomplete statements some 40+ times and that just seems ridiculous!我的另一个选择是将自动完成语句复制 40 次以上,这看起来很荒谬!

so can somebody please give me some direction?所以有人可以给我一些指导吗? thank you!谢谢你!

Loop over the elements in an each loop so you can isolate instances.循环遍历each循环中的元素,以便您可以隔离实例。

$('[data-autocomplete]').each(function() {
  let xhr,
      $input = $(this),
      field = $input.attr('data-autocomplete');

  $input.autoComplete({
    source: function(term, response) {
      try {
        xhr.abort();
      } catch (e) {}
      xhr = $.get('/api.php', { field: field, search: term}, response);
    }
  });

});

The first thing I notice is that you have an erroneous comma after the variable "term" in your get call:我注意到的第一件事是在get调用中的变量“term”后面有一个错误的逗号:

xhr = $.get( '/api.php', 
  { 
    field: $(this).attr('data-autocomplete'),
    search: term, <-- code-breaking comma.
  }, function(data){ response(data); });

It's also possible that your get call's reference to this is no longer refferring to the expected object.您的get调用对this的引用也可能不再指向预期的对象。

Try something like this instead:试试这样的:

$( window ).on( 'load', function () {

    let xhr, me = this;

    $( '[data-autocomplete]' ).autoComplete( {

        source: function( term, response ) {

            try { xhr.abort(); } catch( e ){}

            xhr = $.get(  '/api.php', 
                { 
                    field: $( me ).attr( 'data-autocomplete' ), 
                    search: term
                }, 
                function( data ){ response( data ); 
            } );
            
        }    

    } );

} );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM