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

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

问题描述

是否可以禁止在 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天全站免登陆