自动完成功能不允许输入自由文本? [英] Autocomplete disallow free text entry?

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

问题描述

是否可以禁止在JQuery UI自动完成小部件中输入自由文本?

Is it possible to disallow free text entry in the JQuery UI autocomplete widget?

例如,我只希望允许用户从自动完成列表中显示的项目列表中进行选择,而不希望他们能够写一些随机文本.

eg I only want the user to be allowed to select from the list of items that are presented in the autocomplete list, and dont want them to be able to write some random text.

在演示/文档中我没有看到任何描述如何执行此操作的内容.

I didn't see anything in the demos/docs describing how to do this.

http://jqueryui.com/demos/autocomplete/

我正在使用像这样的自动完成功能

I'm using autocomplete like this

$('#selector').autocomplete({
    source: url,
    minlength: 2,
    select: function (event, ui) {
        // etc
    }

推荐答案

一种方法是在文本提交(如果您使用的是表单)上使用其他验证,以突出显示错误(如果文本不是有效的文本之一)选项.

One way would be to use additional validation on form submit (if you are using a form) to highlight an error if the text isn't one of the valid option.

另一种方法是将附加到自动完成的更改事件上,即使未选择选项,该事件也会被触发.然后,您可以进行验证以确保用户输入位于列表中,如果没有,则显示错误.

Another way would be to attach to the auto complete's change event which will get fired even if an options isn't selected. You can then do validation to ensure the user input is in your list or display an error if it is not.

这篇关于自动完成功能不允许输入自由文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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