Jquery 自动完成链接

发布于 2024-11-18 09:01:02 字数 975 浏览 5 评论 0原文

我在我的网站上使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

两个我 2024-11-25 09:01:02

由于我不懂 PHP,所以我无法帮助您处理服务器端代码,但是一旦您了解了自动完成可以使用的不同数据,就应该很容易弄清楚。

对于小部件的数据源,您可以使用 labelvalue 属性指定对象数组。您可以利用 value 属性来存储数据库中的 id,然后为 select 事件定义一个事件处理程序以将用户发送到正确的页面。

这是一个示例:

$("input").autocomplete({
    source: [
        { label: "Home", value: '3' },
        { label: "Admin", value: '4' },
        { label: "Search", value: '55' }
    ],
    select: function(event, ui) {
        /* Prevent the input from being populated: */
        event.preventDefault();
        /* Use ui.item.value to access the id */
        window.location.href = "/index.php?id=" + ui.item.value;
    }
});

这是一个工作(更简单)的示例: 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 and value property. You could leverage the value property to store the id from your database, and then define an event handler for the select event to send the user to the correct page.

Here's an example:

$("input").autocomplete({
    source: [
        { label: "Home", value: '3' },
        { label: "Admin", value: '4' },
        { label: "Search", value: '55' }
    ],
    select: function(event, ui) {
        /* Prevent the input from being populated: */
        event.preventDefault();
        /* Use ui.item.value to access the id */
        window.location.href = "/index.php?id=" + ui.item.value;
    }
});

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.

櫻之舞 2024-11-25 09:01:02

将 echo 标题行更改为 echo

change your echo title line to echo <a href="index.php?id=$id>$title</a>;

云仙小弟 2024-11-25 09:01:02

不确定您是否仍在尝试解决此问题。这是我使用的 解决方案

基本上,你会得到(使用get 方法)您正在传输的 id(或搜索查询),然后根据它执行 mysql 查询。返回的内容之一是 id,然后将其附加到 URL 的一部分。

例如,我需要攻击一个类别:

<script type="text/javascript">
                    function test(){
                        var i = document.createElement('input');
                        i.type = 'hidden';
                        i.name = 'cat';
                        i.value = cat;
                        document.getElementById("searchForm").appendChild(i);
                    }

                </script>

我在表单之后立即使用了它。这会将类别名称附加为提交的 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:

<script type="text/javascript">
                    function test(){
                        var i = document.createElement('input');
                        i.type = 'hidden';
                        i.name = 'cat';
                        i.value = cat;
                        document.getElementById("searchForm").appendChild(i);
                    }

                </script>

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")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文