【问题标题】:Jquery autocomplete does not work with classJquery 自动完成功能不适用于类
【发布时间】:2018-08-30 14:18:39
【问题描述】:

我正在使用来自 Jquery 的自动完成功能,并且当处理程序是 id 时,我一直在使用它,没有任何问题。但是,现在我需要相同的源变量来自动完成大量输入。我的想法是按类替换 id,但它不起作用(没有错误)。 jquery 是这样的:

<script>
$( function() {var all_users = [
        {
        id: "2",
        label: "Claudio"
        },
        {
        id: "3",
        label: "Tom"
        },
        {
        id: "4",
        label: "Brandon"
        },
        {
        id: "5",
        label: "Edgar"
        },
      {
        id: "0",
        label: "0"
      }
    ];

$( ".invitee" ).autocomplete({
  minLength: 0,
  source: all_users,
  focus: function( event, ui ) {
    $( ".invitee" ).val( ui.item.label );
    return false;
  },

  select: function( event, ui ) {
    $( "#invitee_name" ).val( ui.item.label );
    $( "#user_id" ).val( ui.item.id );
    return false;
     }
    })
  } );
  </script>

还有html:

<label>Who was your best friend in Kindergarden</label> 
<input type="text" class="form-control" class="invitee" id="1">

<label>Who was your best friend in High School</label> 
<input type="text" class="form-control" class="invitee" id="2">

<label>Who was your best friend in jail</label> 
<input type="text" class="form-control" class="invitee" id="3">

<input type="text" class="form-control" id="invitee_name">
<input type="text" class="form-control" id="user_id">

如果我将其中一个输入更改为 id="invitee" 并修复处理程序,它运行良好,但仅适用于一个输入。我怎样才能解决这个问题而不需要不知疲倦地复制'$(“.invitee”).autocomplete'?

用于测试目的:avhub.teameivi.com/test_autocomplete.php

【问题讨论】:

  • &lt;input type="text" class="form-control" class="invitee" id="1"&gt; 更改为&lt;input type="text" class="form-control invitee" id="1"&gt;。此外,元素 ID 不应以数字开头。
  • 谢谢!从来没想过。请发布答案以将其标记为已接受。

标签: jquery autocomplete jquery-ui-autocomplete


【解决方案1】:

您错误地为&lt;input&gt; 元素分配了类名。应该是

<input type="text" class="form-control invitee" id="1">

【讨论】:

  • 谢谢!这样做了 3 年后仍然是新手。
猜你喜欢
  • 2018-01-13
  • 1970-01-01
  • 2023-03-27
  • 2013-07-16
  • 2011-10-16
  • 1970-01-01
  • 2014-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多