微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将自动填充功能添加到jQuery-UI 1.8.1

这里是我目前拥有的,不幸的是,我似乎无法弄清楚如何使自动填充使用jQuery-UI …它曾经与直线Autocomplete.js
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js" type="text/javascript"></script>
<script src="http://jquery-ui.googlecode.com/svn/tags/latest/external/jquery.bgiframe-2.1.1.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/i18n/jquery-ui-i18n.min.js" type="text/javascript"></script>

<script language="javascript" type="text/javascript">


    var thesource = "RegionsAutoComplete.axd?PID=3"
    $(function () {
        function log(message) {
            $("<div/>").text(message).prependTo("#log");
            $("#log").attr("scrollTop",0);
        }

        $.expr[':'].textEquals = function (a,i,m) {
            return $(a).text().match("^" + m[3] + "$");
        };

        $("#regions").autocomplete({
            source: thesource,change: function (event,ui) {
                //if the value of the textBox does not match a suggestion,clear its value
                if ($(".ui-autocomplete li:textEquals('" + $(this).val() + "')").size() == 0) {
                    $(this).val('');
                }
                else {
                    //THIS IS CURRENTLY NOT "LOGGING" THE "UI" DATA
                    //IF THE USER DOES NOT EXPLICITLY SELECT
                    //THE SUGGESTED TEXT
                    //PLEASE HELP!!!
                    log(ui.item ? ("Selected: " + ui.item.value + " aka " + ui.item.id) : "nothing selected,input was " + this.value);
                }
            }
        }).live('keydown',function (e) {
            var keyCode = e.keyCode || e.which;
            //if TAB or RETURN is pressed and the text in the textBox does not match a suggestion,set the value of the textBox to the text of the first suggestion
            if ((keyCode == 9 || keyCode == 13) && ($(".ui-autocomplete li:textEquals('" + $(this).val() + "')").size() == 0)) {
                $(this).val($(".ui-autocomplete li:visible:first").text());
            }
        });
    });


</script>

后端的JSON看起来像这样

[
    { "label": "Canada","value": "Canada","id": "1" },{ "label": "United States","value": "United States","id": "2" },]

我使用了答案here来获得mustMatch的工作,但不幸的是,如果我从输入框中输入“tab”,或者如果我完全输入单词,而不是实际选择建议的文本,我将获得“没有选择”的响应,而不是一个价值和身份证.

有没有人知道如何在您实际没有选择该字段时从自动填充中提取ID?

基本上,我正在寻找的是类似于月(本地):示例在这里http://jquery.bassistance.de/autocomplete/demo/

但是显然使用jQuery-UI而不是jquery.autocomplete.js

解决方法

自动填充已经在JQuery UI版本的autocomplete中被弃用,并且不再支持jquery.autocomplete插件.

Here is a link to a github solutiuon.这将自动选择列表中的第一个项目,如果您选择了该字段,并将“selectFirst:true”设置为自动完成调用的选项.

(function( $) {

$( ".ui-autocomplete-input" ).live( "autocompleteopen",function() {
    var autocomplete = $( this ).data( "autocomplete" ),menu = autocomplete.menu;

    if ( !autocomplete.options.selectFirst ) {
        return;
    }

    menu.activate( $.Event({ type: "mouseenter" }),menu.element.children().first() );
});

}( jQuery ));

原文地址:https://www.jb51.cc/jquery/179556.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐