【问题标题】:jQuery multiple autocomplete with different input IDs具有不同输入ID的jQuery多个自动完成
【发布时间】:2012-12-23 17:51:49
【问题描述】:

我使用 jQuery 自动完成功能并有多个具有不同 ID 的输入字段,它们是从 MySQL 数据库中填充的。

  $("#CCU1").autocomplete({
  minLength : 1,
  source: function( request, response ) {
    $.ajax({
      url:"<?php echo site_url().'gsimc/autocomplete'; ?>",
      dataType: 'json',
      data: { 
        term  : $("#CCU1").val(),
        column: 'course',
        tbl   : 'tbl_courses'
      },
      success: function(data){
        if(data.response == 'true') {
          response(data.message);
        }
      }
    });
  }
});

输入字段的 ID 为 CCU1...CCU5,name='course'。知道如何自动完成五个输入字段而不是对每个字段进行硬编码吗?

Course1: <input type='text' name='course[]' id='CCU1'/><br />
Course2: <input type='text' name='course[]' id='CCU2'/><br />
Course3: <input type='text' name='course[]' id='CCU3'/><br />
Course4: <input type='text' name='course[]' id='CCU4'/><br />
Course5: <input type='text' name='course[]' id='CCU5'/><br />

【问题讨论】:

  • 一个可以使用的模式:像.autocompleteHandler这样为每个元素添加一个dummyclass,并将该类称为选择器$('.autocompleteHandler').autocomplete(),并在函数内部使用对$(this)的引用。跨度>

标签: jquery ajax jquery-ui jquery-ui-autocomplete


【解决方案1】:

假设您的上述代码适用于其中之一,您可以这样做:

$("[id^=CCU]").each(function(){
    $(this).autocomplete({
      minLength : 1,
      source: function( request, response ) {
        $.ajax({
          url:"<?php echo site_url().'gsimc/autocomplete'; ?>",
          dataType: 'json',
          data: { 
            term  : $(this).val(),
            column: 'course',
            tbl   : 'tbl_courses'
          },
          success: function(data){
            if(data.response == 'true') {
              response(data.message);
            }
          }
        });
      }
    });
});
​

【讨论】:

    【解决方案2】:

    使用 $(this) 而不是硬编码 ID:

    term: $("#CCU1").val(),
    

    替换为:

    term: $(this).val(),
    

    完整代码:

    $("[id^=CCU]").each(function(){
        $(this).autocomplete({
          minLength : 1,
          source: function( request, response ) {
            $.ajax({
              url:"<?php echo site_url().'gsimc/autocomplete'; ?>",
              dataType: 'json',
              data: { 
                term  : $(this).val(),
                column: 'course',
                tbl   : 'tbl_courses'
              },
              success: function(data){
                if(data.response == 'true') {
                  response(data.message);
                }
              }
            });
          }
        });
    });
    

    【讨论】:

    • 我得到一个错误:Uncaught TypeError: Cannot call method 'toLowerCase' of undefined
    • 上述脚本中没有用到toLowerCase函数。你能告诉我们完整的代码吗?
    • 错误出现在 jquery.min.js 中,但我没有问题只显示一个输入字段的自动完成。
    • 这个我真的想不通。我尝试了你们所有人建议的一切,但仍然得到未捕获的类型错误。也许我需要先把这个睡出来。 :(
    • this inside $.ajax 并不是你想象的那样。在调用 $.ajax 之前声明一个变量 var $this=$(this) 并改用该变量
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    相关资源
    最近更新 更多