- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题:
我的 JQuery 自动完成功能返回在数据库中找到的两个关键字。我希望用户在搜索时看到两者,但在选择选项时,只应选择一个关键字并将其插入作为值。
HTML 代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Demo</title>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/base/minified/jquery-ui.min.css">
</head>
<body>
<form action="" method="post">
<p>
<label>Country:</label>
<input type='text' name='country' value='' class='auto'>
</p>
</form>
<script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="//code.jquery.com/ui/1.10.1/jquery-ui.min.js"></script>
<script type="text/javascript">
$(function() {
//autocomplete
$(".auto").autocomplete({
source: "search.php",
minLength: 1
});
});
</script>
</body>
</html>
PHP 代码:
<?php
if (isset($_GET['term']))
{
$return_arr = array();
try {
$conn = new PDO("mysql:host=".DB_SERVER.";dbname=".DB_NAME, DB_USER, DB_PASSWORD);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare('SELECT * FROM airports WHERE name LIKE :term OR iata LIKE :term');
$stmt->execute(array('term' => '%'.$_GET['term'].'%'));
while($row = $stmt->fetch()) {
$return_arr[] = $row['iata'] . ' (' . $row['name'] . ')';
}
}
catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
echo json_encode($return_arr);
}
?>
输出:
如您所见,整个名称显示在输入字段中。但我只希望缩写出现在那里。问题是:
如何让用户在搜索时看到全名,但在用户做出选择后才选择缩写?
所需输出:
仅获取输入字段中机场的缩写(例如 JFK)。
最佳答案
解决方案在这里找到:jQuery UI autocomplete shows value instead of label in the input field
$return_arr[] = array('value' => $row['iata'], 'label' => $row['iata'] . ' (' . $row['name'] . ')');
jQuery 使用 label
和 value
的组合,这是解决方案。
关于javascript - 从 JQuery 自动完成中仅获取第一个术语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37303553/
我正在使用 drupal 6.20 以及一些模块,包括面板、 View 和其他一些模块.. 问题是,每当我尝试启用面板提供的分类法覆盖页面时,我都会收到此错误,我修改了它等等,我似乎找不到一种方法来启
我正在寻找在类(非静态)中声明的实例变量的替代 OO/Java 术语,或者更具体地说,在用 JPA 注释“装饰”的 Java 类中声明的实例变量: @Entity @Table(name = "Dep
字母“t”在 LISP 中是什么意思? 例如: (defun last2 (lst) (cond ((null lst) nil) ((null (cdr lst)) (car l
我是 Java 的新手,想了解更多。我有一个当前问题想要得到解答,但我也想知道该技术指的是什么,以便我可以做一些进一步的阅读。 我目前有这样的东西: public class BasicActivit
是否有针对 HTML 标签的术语来区分哪些应该有结束标签,哪些不应该? 例如,和 应该有伴随 和 标签。 另一方面,和 不应该。 第一组叫什么,第二组叫什么? 最佳答案 我相信 是一个“空元素”,而不
基本上,问题已经总结在标题中。如果我们在不同的命名空间中有两个具有相同签名(即相同的名称、参数类型和顺序、泛型类型参数编号)的方法/函数,这算不算重载? 这是一个 C# 问题,但我很想为不同的 OOP
在 OO 范式中,我们选择使用类,因为它们可以帮助我们分解系统,并提供很好的附带好处,例如封装、职责分离、继承、模块化等。 如果我们在组件级别查看软件系统,我们是否可以简单地以相同的概念方式对待组件,
创建拉取请求和打开拉取请求之间有区别吗? 创建拉取请求的一些短语示例: 创建一个拉取请求以提议和协作对存储库的更改。 您可以在创建拉取请求时指定要将更改合并到哪个分支。 要创建草稿拉取请求,请使用下拉
我发现this script可以循环遍历.csv文件并将它们组合成一个Excel工作表。然后,我创建了第二个脚本,以如下方式调用该脚本: echo "Combining .csv files into
我忘记了 javascript 中用来描述特定现象的术语。它与内联函数中访问变量的方式有关。我也不太明白这个理论。我依稀记得下面的代码 for(var c = 0; c< 10; c++) { a
如何清除Java中的标准输入(术语)? 一点历史:我正在编写一个“反射”程序,算法非常简单: wait a random amount of time print "press enter" read
给定以下代码,是否存在一个静态方法的名称/术语,它为每个现有实例调用同名的实例方法? 这是任何编程语言的常见做法吗? 用例是能够进行一个函数调用并确保所有实例都受到影响,而无需为该方法复制代码。 注意
这个问题在这里已经有了答案: Accessing nested JavaScript objects and arrays by string path (44 个答案) 关闭 6 年前。 我需要澄
我的目标是从给定的输入文件中读取每行的第一个元素/术语,然后根据第一个元素是什么来决定要做什么(使用 if-else 构造)。 IE。如果第一个元素/单词恰好是“the”(如下面的代码中所述),那么我
在 Java 中,对象 可以有一个运行时类型(这是它创建时的类型)和一个转换类型(您将其转换为的类型)。 我想知道这些类型的正确名称是什么。例如 class A { } class B extends
根据 Python 2.7.12 文档,User-defined methods : User-defined method objects may be created when getting a
据我所知,nbsp(不间断空格)是这样的:。但制表符 (\t) 也是不间断空格,对吗?我的意思是它不会创建新行。 如果上述所有内容都是正确的,那么如何调用可以包含 或 \t 的变量?像 tabOrNb
我使用 GAS 已经有一段时间了,但没有很强的 Javascript 背景,并且在忽略大小写的情况下按字母顺序对工作表进行排序时遇到了问题。我做了一些搜索,并根据 SO 中的其他公开答案和其他一些来源
我是初学者,我在编程中发现了术语指针的几种定义。我想知道哪一个是正确的(也许两个都是)? a - 指针是保存内存地址的变量。鉴于此定义,在以下代码 char *msg; 中,我们可以说变量 msg 是
给定以下分支 A---B---C topic (HEAD) / D---E---F---G master 并运行命令 git rebase master 这是否意味着,我们是 将 t
我是一名优秀的程序员,十分优秀!