jQuery的自动完成输入关键问题 [英] jquery autocomplete enter key issue

查看:167
本文介绍了jQuery的自动完成输入关键问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然这可能是一个问题,重复的,但我没能找到一个解决方案。我有使用jQuery自动完成搜索文本框。当我输入文本框的东西,它显示出其中没有选择任何建议。

Though it may be a duplicate question but i'm not able to find a solution. I have a text box for searching using jQuery autocomplete. When I type something in text box, it is showing the suggestions out of which none is selected.

我发现的 selectFirst:真正的现在不支持。如果我选择下拉列表和preSS输入项目,它工作正常。

I found out that selectFirst: true is not supported now. If I select an item in the drop down list and press enter, it is working fine.

问题是,如果用户输入应用作为苹果和presses回车键时,它会尝试与应用提交表单。该表格​​只能与其中一个值从下拉列表提交。

The problem is that if user enters app for apple and presses the enter key, it tries to submit the form with app. The form should only be submitted with one of the values from the drop down list.

我想禁用表单提交上pressing进入,这样我可以从JavaScript提交表单,但无法找到一个解决方案。请帮我在这:

I tried to disable the form submit on pressing enter so that I could submit form from JavaScript but couldn't find a solution. Please help me on this:

下面是我的code:

$("#searchtext").autocomplete({
    source: path + 'autocompletesearch?category=' + $("select[name='category'] option:selected").val(), 
    width: '500px', 
    matchContains: true, 
    select: function(event, ui){
        $(".searchform").attr("action", path + "fullproductinfo/" + ui.item.id);
        $("input[name='searchid']").val(ui.item.value);
        $(".searchform").submit();
    }
});

和我的方式:

        <form method="post" action="search" class="searchform">
            <div class="searchdiv">
                <input type="text" id="searchtext" name="searchtext"
                    placeholder="Search a product or a brand" autocomplete="off"/>
            </div>
            <div class="searchbutton">
                <input type="button" name="btnsearchproduct" value="Search" />
            </div>
            <div class="clear"></div>
        </form>

需要注意的是,而不是提交,所以默认输入pressing不应该的形式工作。它是自动完成我想这是对提交提交表单。

Note that there is and not 'submit', so default enter pressing should not work for form. It is the autocomplete i guess that is submitting the form on submit.

在此先感谢

推荐答案

如果您使用的是这样的:的http:// jQueryUI的.COM /自动/

If you are using this: http://jqueryui.com/autocomplete/

然后,你想先项目自动对焦,您现在可以使用该选项:

Then you want first item to autoFocus, you can use that option now:

$( ".selector" ).autocomplete({ autoFocus: true });

在文档中提到

http://api.jqueryui.com/autocomplete/

修改

既然你已经进入关键的问题,你可以尝试这样的:

Since you have enter key issue, you can try like this:

$(".selector").keydown(function(event){
    if(event.keyCode == 13) {
      if($(".selector").val().length==0) {
          event.preventDefault();
          return false;
      }
    }
 });

这篇关于jQuery的自动完成输入关键问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆