JQuery的自动完成:在选择提交表格? [英] JQuery Autocomplete: Submit Form on selection?

查看:267
本文介绍了JQuery的自动完成:在选择提交表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

传统的HTML表单上使用jQuery自动完成。

Using JQuery Autocomplete on a traditional HTML form.

试图提交表单(老式的方式)时做出选择。

Trying submit the form (the old-fashioned way) when a selection is made.

不过,在输入框中得到填补出来,然后我必须做出preSS回归一2次,或点击提交按钮。

But the input box gets filled out and then I have to make press "return" a 2nd time, or click the submit button.

我试过了几个例子SO,但我不能让他们的工作。

I've tried a few SO examples, but I could not get them to work.

你如何自动提交表单做出选择的时候?

How do you submit the form automatically when the selection is made?

推荐答案

更新:我终于想通这一个,低于code应该做的伎俩。出于某种原因,变更回调不工作,但关闭&安培; 选择回调做。使用选择更好,因为关闭也会如果该字段失去焦点时调用。

UPDATE: I finally figured this one out, the code below should do the trick. For some reason the change callback was not working, but the close & select callbacks do. Using select is better, since close will also be called if the field loses focus.

$(function() {
    $("#searchField").autocomplete({
        source: "values.json",
        select: function(event, ui) { 
            $("#searchForm").submit(); }
    });
});

其他更新:确定,但也与选择回调的一个问题,这是在默认情况下(在上面的code )如果您遍历自动完成下拉与键盘,并用回车键选择,提交表单前输入被改变。但是,如果你用鼠标选择它,表单提交的输入被改变之前,因此提交的值是用户输入的正是(她从自动完成下拉列表中选择的不是)。这似乎工作的woraround是:

ANOTHER UPDATE: Ok, there's also a problem with the select callback, which is that by default (in the code above) if you traverse the autocomplete drop down with the keyboard, and select with the enter key, the input is changed before the form is submitted. However, if you select it with the mouse, the form is submitted just before the input is changed, so the value submitted is just what the user typed (not what she selected from the autocomplete dropdown). The woraround that seems to work is:

$("#searchField").autocomplete({
    source: "values.json",
    minLength: 2,
    select: function(event, ui) { 
        $("#searchField").val(ui.item.label);
        $("#searchForm").submit(); }
});

这篇关于JQuery的自动完成:在选择提交表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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