jQuery的自动完成多个不同的输入标识多个、自动完成、标识、不同

2023-09-10 20:57:44 作者:鉮經嬡仩皛痴

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

  $(#CCU1)。自动完成({
  的minLength:1,
  来源:功能(请求,响应){
    $阿贾克斯({
      网址:< PHP的回声SITE_URL()'gsimc /自动完成';>?。?
      数据类型:JSON,
      数据: {
        长期:$(#CCU1)VAL()。
        专栏:当然,
        TBL:tbl_courses
      },
      成功:功能(数据){
        如果(data.response =='真'){
          响应(data.message);
        }
      }
    });
  }
});
 

输入的字段有CCU1 ... CCU5,NAME ='当然'的ID。任何想法如何自动完成硬编码每一个的五个输入字段呢?

  Course1:LT;输入类型=文本名称=当然[]'ID ='CCU1'/>< BR />
Course2:LT;输入类型=文本名称=当然[]'ID ='CCU2/>< BR />
Course3:LT;输入类型=文本名称=当然[]'ID ='CCU3'/>< BR />
Course4:LT;输入类型=文本名称=当然[]'ID ='CCU4'/>< BR />
Course5:LT;输入类型=文本名称=当然[]'ID ='CCU5'/>< BR />
 

解决方案

假设你上面code正在为其中之一,你可以做:

  $([编号^ = CCU])。每个(函数(){
    $(本).autocomplete({
      的minLength:1,
      来源:功能(请求,响应){
        $阿贾克斯({
          网址:< PHP的回声SITE_URL()'gsimc /自动完成';>?。?
          数据类型:JSON,
          数据: {
            长期:$(本).VAL()
            专栏:当然,
            TBL:tbl_courses
          },
          成功:功能(数据){
            如果(data.response =='真'){
              响应(data.message);
            }
          }
        });
      }
    });
});

 
JQuery

I use jQuery autocomplete and have multiple input fields with different IDs, and they are populated from a MySQL database.

  $("#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);
        }
      }
    });
  }
});

The input fields has IDs of CCU1...CCU5, name='course'. Any idea how to autocomplete the five input fields instead of hardcoding each one?

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 />

解决方案

Assuming that your above code is working for one of them, you could do:

$("[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);
            }
          }
        });
      }
    });
});
​