首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery自动完成输出所有结果,而不是匹配字符串

jQuery自动完成输出所有结果,而不是匹配字符串
EN

Stack Overflow用户
提问于 2013-06-28 07:03:36
回答 2查看 171关注 0票数 1

我更新了JSON和jQuery。我需要做一个jQuery自动完成,为此我说:

代码语言:javascript
复制
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>


<script>
jQuery(document).ready(function($){
    $('#executives').autocomplete({source:'search_executives.php', minLength:2});
});
</script>

在HTML中:

代码语言:javascript
复制
<div class="rows">
    <div class="labels">Search</div>
    <div class="textboxs" style=" width: 175px;"><input id="executives" name="executive" /></div>
</div>

我的search_executives.php是:

代码语言:javascript
复制
<?php
session_start();
    if(empty($_SESSION['userid'])){
        header('Location:index.php');
        exit();
    }
include_once('include/config.php'); 

$qry = "SELECT employee_id,fname,mname,lname FROM personal_details WHERE deg_id = '1'";

$res = $dbo->executeQuery( $qry ) ;

//$results[] = array('label' => $row['name']);

for( $i = 0; $i < count( $res ); $i++ )
{
    $result[] = array( 'label' => $res[$i]['fname'].' '.$res[$i]['mname'].' '.$res[$i]['lname'] , 'value' => $res[$i]['employee_id'] ) ;
}

echo json_encode($result);
?>

现在我的问题是-

  1. 它输出所有结果,它不依赖于我输入的搜索字符串。
  2. 它在HTML中打印value (例如3)。我需要打印这些高管的名字,并以他的employee_id作为他的价值。

注:第一部分解决了。我忘了在LIKE中添加SQL条件!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-28 07:47:26

您必须将输入的值设置为label,然后有另一个字段在选定项时存储employee_id

代码语言:javascript
复制
select: function(e, ui) {
  e.preventDefault();
  this.value = ui.item.label;
  $('#other_field').val(ui.item.value);
}

下面是一个例子

票数 1
EN

Stack Overflow用户

发布于 2013-06-28 07:25:09

您正在使用输入文本框.

文本框的value属性出现在前端文本框中,您不能更改它。在你的情况下是employee_id。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17359069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档