2017-04-22 54 views
-2

我一直在尝试将我的自动提示脚本从jquery自动填充1.1转换为最新的自动完成用户界面,但似乎很难做到。如果有人能帮忙,会很高兴。 PHP代码:转换旧的jQuery自动完成与最新的jQuery自动完成用户界面

include("dbconnect.php"); 
$term = trim(strip_tags(strtolower($_GET['term']))); 
if (!$term) 
{ 
} 
else 
{ 
    $query = "SELECT DISTINCT city as cityname,id as cityid FROM city where LCASE(city) like '%".$term."%'"; 
    $results = mysql_query($query); 
while ($row = mysql_fetch_array($results,MYSQL_ASSOC)) 
    { 
    $row_set[]=(int)$row['cityid']; 
    $row_set[]=htmlentities(stripslashes($row['cityname'])); 

    } 

} 
echo json_encode($row_set);//format the array into json data 

这:

include("dbconnect.php"); 
$term = trim(strip_tags(strtolower($_GET['term']))); 
$city = intval($_GET['city_val']); 

if (!$term) 
{ 
} 
else 
{ 
    $query = "SELECT DISTINCT locality as localityname,id as localityid FROM locality where cityid='{$city}' and LCASE(locality) like '%".$term."%'"; 
    $results = mysql_query($query); 
    while ($row = mysql_fetch_array($results,MYSQL_ASSOC)) 
    { 

    $row_set[]=htmlentities(stripslashes($row['localityname'])); 
    $row_set[]=(int)$row['localityid']; 


    } 

} 
echo json_encode($row_set);//format the array into json data 

和JS:

// Set default values to -1 and disable locality 
    $("#city").focus(); 
    $("#city_val").val(-1); 
    $("#locality").val("Select City First"); 
    $("#locality_val").val(-1); 
    document.getElementById('locality').disabled =true; 



    // start city part 
$("#city").autocomplete({ 
    source: 'new_suggestion.php', 
    minLength: 2, 

}); 

$("#city").result(function(event,data,formatted) 
{ 
    //$("#city_val").val(data[1]); 

    if (document.getElementById('city').value == "") 
      { 
      $("#city").val("National Search"); 
      $("#city_val").val(-1); 
      $("#locality").val("Select City First"); 
      $("#locality_val").val(-1); 
      document.getElementById('locality').disabled =true; 
      var ct="-1"; 
      } 
    else 
      { 
      $("#city_val").val(data[1]); 

      document.getElementById('locality').disabled =false; 
      $("#locality_val").val(-1); 
      $("#locality").val(""); 
      var ct=data[1] 
      } 

// if city value found start locality here 
if (ct != -1) 
{ 
// locality starts // 
$("#locality").val(""); 
$("#locality_val").val(-1); 

$("#locality").autocomplete({ 
    source: "new_suggestion2.php?city_val="+ct, 
    minLength: 2, 
}); 

$("#locality").result(function(event, data, formatted) 
    { 

     if (document.getElementById('locality') == "") 
     { 
     $("#locality_val").val(-1); 
     $("#locality").val("true"); 
     } 
     else 
     { 
     $("#locality_val").val(data[1]); 
     } 
    }); 
// locality ends // 
} // end of if ct 
+1

您提供了一个合理数量的代码和无意义的问题陈述。 *“发现很难做”*告诉我们绝对没有价值。看到[问] – charlietfl

回答

0
$('#my-search-input').autocomplete({ 
minLength: 1, 
delay: 1000, 
source: function(request, response) { 
    $.get('/api/get/autocomplete/', { 
    search: encodeURIComponent(request.term), 
    time: $.now() 
    }, 
    function(data) {   
     response(decodeURIComponent(dat)); 
    }, 'json'); 
}); 

你必须要使用请求和响应参数试图用时与自动完成ajax。往上看。

+0

谢谢你会尝试 – user1762269