文本字段自动完成-仅允许在自动完成选项中提交 [英] text field auto-complete - only allow submissions within autocomplete options

查看:42
本文介绍了文本字段自动完成-仅允许在自动完成选项中提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找仅在指定的数据数组内允许自动完成的代码.因此,仅允许输入自动完成数组中的数据.不确定从哪里开始寻找.

I am looking for code that allows an autocomplete, only within a specified data array. So only data within the auto complete array is allowed to be entered. Not sure where to start looking.

谢谢!

推荐答案

以下解决方案,它利用 jQuery UI Autocomplete 就足够了-当然,根据您的特定要求,可能需要进一步调整.

The following solution, which utilises jQuery UI Autocomplete, should suffice - of course it might need some further tweaking depending on your specific requirements.

标记:

<input id="autocomplete"/>
<br />
<input id="submit" type="submit" disabled="disabled"/>

脚本:

var source = ['One', 'Two', 'Three', 'Four'];

$("input#autocomplete").autocomplete({
    source: source,
    select: function(event, ui) {
        $("#submit").removeAttr("disabled");
    }
}).keyup(function() {
    var $parentContext = $(this);
    var matches = $.grep(source, function(n, i) {
        return $parentContext.val().toLowerCase() == n.toLowerCase();
    });
    $("#submit").attr("disabled", !matches.length);
});

演示: http://jsfiddle.net/karim79/Ezbns/

进一步阅读:

  • http://jqueryui.com/demos/autocomplete/
  • http://api.jquery.com/jQuery.grep/

这篇关于文本字段自动完成-仅允许在自动完成选项中提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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