Jquery 自动完成链接
我在我的网站上使用 jquery 的自动完成功能。现在,它生成的菜单包含您可以单击的链接。
但是,当您单击它们时,它们只会进入文本字段。我希望能够单击链接(或按 Enter 键)并进入另一个页面,特别是更改 URL 的 get 变量。
为了让事情变得更复杂一些,下拉列表中的每个项目都由我的 mysql 数据库分配了一个 ID。每个项目应访问的链接是 index.php?id=$id
,其中 $id
是数据库中的 ID。
由于菜单中有数百个项目,如何更改当前代码以包含重定向到 index.php?id=$id
从我的网站获取 $id
的链接数据库?
代码: 来自 http://jqueryui.com/demos/autocomplete/ 的标准代码
<script>
$(function() {
var availableTags = [
<?php
include 'connect.php';
$result=mysql_query("SELECT * FROM searchengine");
while ($row=mysql_fetch_assoc($result)){
$title=$row['title'];
$id=$row['id'];
echo "\"$title\",";
}
?>
];
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>
PS 我在 jquery 中使用 php生成变量列表。
I use jquery's autocomplete on my site. Right now the menu it produces has links that you can click.
However, when you click them they simply go into the text field. I would like to be able to click on the link (or press enter) and be taken to another page, specifically change the URL's get variable.
To make this a little more complicated, each item in the drop down is assigned an ID by my mysql database. The link that each item should go to is index.php?id=$id
where $id
is its ID from the database.
Since I have hundreds of items in the menu, how can I alter the current code to include links that redirect to index.php?id=$id
getting $id
from my database?
CODE:
standard code from http://jqueryui.com/demos/autocomplete/
<script>
$(function() {
var availableTags = [
<?php
include 'connect.php';
$result=mysql_query("SELECT * FROM searchengine");
while ($row=mysql_fetch_assoc($result)){
$title=$row['title'];
$id=$row['id'];
echo "\"$title\",";
}
?>
];
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>
P.S. I use php inside the jquery to generate a list of variables.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于我不懂 PHP,所以我无法帮助您处理服务器端代码,但是一旦您了解了自动完成可以使用的不同数据,就应该很容易弄清楚。
对于小部件的数据源,您可以使用
label
和value
属性指定对象数组。您可以利用value
属性来存储数据库中的id
,然后为select
事件定义一个事件处理程序以将用户发送到正确的页面。这是一个示例:
这是一个工作(更简单)的示例: http://jsfiddle.net/j2JUb/
作为旁注,如果您有的话,我会考虑使用自动完成提供的 远程数据源 选项大量可能的结果。
Since I don't know PHP, I can't help you with the server-side code, but it should be easy enough to figure out once you understand the different data that autocomplete can use.
For the widget's data source, you can specify an array of objects with a
label
andvalue
property. You could leverage thevalue
property to store theid
from your database, and then define an event handler for theselect
event to send the user to the correct page.Here's an example:
Here's a working (simpler) example: http://jsfiddle.net/j2JUb/
As a side note, I would consider using the remote datasource option that autocomplete provides if you have a ton of possible results.
将 echo 标题行更改为 echo
change your echo title line to echo
<a href="index.php?id=$id>$title</a>;
不确定您是否仍在尝试解决此问题。这是我使用的 解决方案
基本上,你会得到(使用get 方法)您正在传输的 id(或搜索查询),然后根据它执行 mysql 查询。返回的内容之一是 id,然后将其附加到 URL 的一部分。
例如,我需要攻击一个类别:
我在表单之后立即使用了它。这会将类别名称附加为提交的 url 的一部分。 (例如&cat=dfgdfg)。你可以用你的 id 做同样的事情。
对我来说最有效的选项是,在提交时,将再次查询数据库,并使用 id(以及重定向)将您带到所需的页面。
希望这是有道理的。
总之:您处理它的方式是获取与您想要的页面关联的 id,并将其附加到您的提交任务(例如 onsubmit="search.php=$id")
Not sure if you're still trying to get this sorted. Here's the solution that I used
Basically, you get (using the get method) the id you're transmitting (or search query) and then do a mysql query based on it. One of the things that is returned is the id and then you attach this to part of your URL.
For example, I needed to attack a category:
I used that right after my form. This attached the category name as a part of the submitted url. (eg. &cat=dfgdfg). You could do the same with your id.
The option that worked best for me was that on submit, the database would again be queried and the id (along with a redirect) was used to take you to the page that was wanted.
Hope that makes sense.
In summary: The way you deal with it is getting the id that is associated with the page you want and this is attached to your submit task (eg. onsubmit="search.php=$id")