简体   繁体   English

JQuery 自动完成表单提交

[英]JQuery Autocomplete Form Submission

I have managed to implement the jQuery autocomplete plugin on my website but was wondering if it is possible to make the form auto-submit once the user selects an item from the search.我已经设法在我的网站上实现了jQuery自动完成插件,但想知道是否可以在用户从搜索中选择一个项目后自动提交表单。 I have the following set-up:我有以下设置:

HTML Form: HTML 表单:

<form class="quick_search" action="../include/search.php" method="post">
    <input type="text" name="search" id="search" value="Search...">
</form>

JavaScript: JavaScript:

$().ready(function() {
    $("#search").autocomplete("../include/search.php", {
        width: 350,
        selectFirst: false
    });
});

I have also included the jQuery and Autoplugin scripts.我还包含了jQueryAutoplugin脚本。 The search.php file contains a list of the search options. search.php 文件包含搜索选项列表。

At the moment, the search works correctly and I just need it to submit the form once an item is selected from the list that appears.目前,搜索工作正常,我只需要在从出现的列表中选择一个项目后提交表单即可。 I tried to use the onClick and onSelect options within the search field but neither of these worked correctly.我尝试在搜索字段中使用 onClick 和 onSelect 选项,但这些都没有正常工作。

Any help would be much appreciated (I don't really understand js)!任何帮助将不胜感激(我不太了解 js)! Thanks.谢谢。

Just found out you CAN use select but not on ui.item but on ui.item.value刚刚发现您可以使用 select 但不能在 ui.item 上,而是在 ui.item.value 上

$(document).ready(function() {
    $(function() {
        $("#search_box").autocomplete({
            source: ['Google', 'Yahoo', 'StackOverflow'],
            select: function(event, ui) {
                $(event.target).val(ui.item.value);
                $('#search_form').submit();
                return false;
            },
            minLength: 1
        });
    });
});​

See this fiddle http://jsfiddle.net/uXHCQ/看到这个小提琴http://jsfiddle.net/uXHCQ/

Thanks to @tim peterson for jQuery-UI Autocomplete submitting form onclick of item from dropdown list感谢@tim peterson 的jQuery-UI 自动完成提交表单 onclick 从下拉列表中的项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM