简体   繁体   English

使用带有多个输入字段的 jquery-ui 自动完成功能

[英]using jquery-ui autocomplete with multiple input fields

good afternoon all!大家下午好!

i spared a lot of time, read all posts on stackoverflow... and i am not able to make autocomplete working with multilpe input fields.我节省了很多时间,阅读了 stackoverflow 上的所有帖子……但我无法使用 multilpe 输入字段进行自动完成。 I tried to attrib a 'autoc' class to each input, i use a different id for each field (in fact the inedx of the php loop generating fields).我试图为每个输入分配一个“autoc”类,我为每个字段使用不同的 id(实际上是 php 循环生成字段的 inedx)。 I do not ask someone to do the job for me.... just a working example.我不要求别人为我做这项工作......只是一个工作示例。

Thanks in advance.提前致谢。

PS : I apologize for my poor english... PS:我为我糟糕的英语道歉......

now follows a piece of html :现在跟随一段 html :

    <input id="search_ctO" class="autoc" type="text" name="search_ct[]">
    <input id="search_ct1" class="autoc" type="text" name="search_ct[]">
    <input id="search_ct2" class="autoc" type="text" name="search_ct[]">
    ....
    <input id="search_ctn" class="autoc" type="text" name="search_ct[]">

and jquery :和 jquery :

    $('.autoc').on("focus", function()   
      $(this).autocomplete({
       minLength: 2,
       source: 'liste_contact.php',
       select: function( event, ui ) {  
         $('.autoc #search_ct').val( ui.item.label ); //id="search_ct'.$i.'
         $(".autoc #contact_id").val( ui.item.value ); //
         $("autoc #contact_description").val( ui.item.desc );
         return false;
       },  
      change: function(){ 
         var servi = $("#service_id").val();
         var hop = $('#hop').val();
         var contact = $("#contact_id" ).val();
         $.ajax({
           url: 'ajout_contact.php',
           data: "serv="+ servi+"&hopit=" + hop+"&contact="+ contact+"",// on envoie la requete d'ajout de contact

         success: function() {
               $("#search_ct").val('');
               // location.reload(true);         
       }

Without knowing the exact HTML and the object array passed to autocomplete source, it is difficult to make your code exactly.如果不知道确切的 HTML 和传递给autocomplete源的对象数组,就很难准确地编写代码。

However, you have asked about working of autocomplete for multiple fields, so here is just a simple example:但是,您已经询问了多个字段的autocomplete功能,所以这里只是一个简单的例子:

HTML HTML

<input id="search_ctO" class="autoc" type="text" name="search_ct[]"/>
<input id="search_ct1" class="autoc" type="text" name="search_ct[]"/>
<input id="search_ct2" class="autoc" type="text" name="search_ct[]"/>
<input id="search_ctn" class="autoc" type="text" name="search_ct[]"/>

JS JS

var tags = ["abc","def","xyz"];
$('.autoc').on("focus", function(){
      $(this).autocomplete({
       minLength: 2,
       source: tags
        });
});

JSFIDDLE DEMO JSFIDDLE 演示

If there is any other thing you want to be included in answer, feel free to comment.如果您想在答案中包含任何其他内容,请随时发表评论。

EDIT编辑

Your code,你的代码,

$('.autoc').on("focus", function() {
    $(this).autocomplete({
        minLength: 2,
        source: 'liste_contact.php',
        select: function( event, ui ) {  
            $('.autoc #search_ct').val( ui.item.label );
            $(".autoc #contact_id").val( ui.item.value );
            $(".autoc #contact_description").val( ui.item.desc );
            return false;
        },  
        change: function() { 
            var servi = $("#service_id").val();
            var hop = $('#hop').val();
            var contact = $("#contact_id" ).val();
            $.ajax({
                url: 'ajout_contact.php',
                data: "serv="+servi+"&hopit="+hop+"&contact="+contact+"",
                success: function() {
                    $("#search_ct").val('');        
                }
            });
        }
    });
});

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

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