选择时获取jQuery自动完成功能以将项目传递到另一个字段 [英] getting jquery autocomplete to pass item to another field when selected

查看:64
本文介绍了选择时获取jQuery自动完成功能以将项目传递到另一个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为此工作了几个小时,它应该可以工作,但是我丢失了一些东西!

I've been working on this for a couple of hours and it should work but i'm missing something!

基本上,我正在使用带有json源的jquery自动完成功能,具有2个值id和description. 说明中应显示建议,并且如果选择了项目并且ID已传递到隐藏字段(该字段当前未隐藏用于测试目的)

basically, i'm using jquery autocomplete with json source, with 2 values id and description. Description should show up in suggestions and if item is selected and ID passed to a hidden field ( the field is not hidden currently for testing purposes)

这是我的代码:

$(function() {
  $("#CurrentProv").autocomplete({
   source: "inc/provider_term.php",
   minLength: 3,
    formatItem: function(data, i, n, value) {   
        return  value.split("|")[1];
          } 
    });
      $("#CurrentProv").result(function(event, data, formatted) {
      if (data)
            $("input#fid").val(data[0]);
      });
});

//PHP有效的json输出

//PHP valid json output

$term = $_GET['term'];

$sql = "SELECT * FROM db.table WHERE PName LIKE '%$term%'";
$res = mysql_query($sql, $conn) or die(mysql_error());

while ($row = mysql_fetch_array($res)) {

$searchArray['value'] = $row['PID'].'|'.$row['PName'];
$search[] = $searchArray;

}

echo json_encode($search);

我已经搜索并完成了各种变体,但仍然无法正常工作!!!我的大脑在关闭!!!!!!!!

I've searched and done various variations and still doesn't work!!! My brain is shutting down!!!!!!!!

推荐答案

首先,切换为使用实际的jQuery UI 自动完成.

First, switch to using the actual jQuery UI autocomplete.

由于不再支持formatItems,因此您必须在服务器端或JSON回调中弄清如何格式化项目.查看此指南以获得帮助.

You'll have to sort out how to format your items on the server side, or in your JSON callback, because formatItems is not supported anymore. Check out this guide for some help.

现在您已经完成了,下面是它的外观:

Now that you've done that, here's how it will look:

$(function() {
 $("#CurrentProv").autocomplete({
   source: "inc/provider_term.php", //or you can use a callback here
   minLength: 3,
   change: function(event, ui) {
      $("input#fid").val(ui.item.value);//or ui.item.desc perhaps
   }
});

});

这篇关于选择时获取jQuery自动完成功能以将项目传递到另一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆