簡體   English   中英

jQuery-UI自動競爭在`Keydown`事件上更改文本框值

[英]Jquery-UI autocompete change textbox value on `Keydown` event

我正在我的項目中使用jquery-UI自動完成功能,我的代碼如下

   function split(val) {
        return val.split(/,\s*/);
    }
    function extractLast(term) {
        return split(term).pop();
    }
    $(".tags").autocomplete({

        source: function (request, response) {
            loc_array = request.term.split(',');
            var term = loc_array[loc_array.length - 1];
            $.ajax({
                url: "/Admin/Tag1/LookUpCompany",
                dataType: "json",
                data: "q=" + term,
                success: function (data) {
                    response($.map(data, function (item) {                  
                        return {
                            value: item.Name,
                            Name: item.Name
                        };
                    }));
                }
            });
        },
        select: function (event, ui) {
            event.preventDefault();
            var terms = split( this.value );
            // remove the current input
            terms.pop();
            // add the selected item
            terms.push( ui.item.value );
            // add placeholder to get the comma-and-space at the end
           // terms.push( "" );
            this.value = terms.join( "," );
            return false;
        },
        minLength: 1
    });

它的工作正常,但有一個問題,當我加載頁面時,我的文本框具有價值
abc,def,ghi,

現在,如果我輸入任何字符,都會以下拉列表的形式給我建議。 如果我單擊它,它將在單擊的值后面附加我的當前值,但是如果我使用鍵盤上的向下鍵向下移動,則它將使用當前選定的值更改整個textbpx值。 如何解決?

謝謝 ,

這是我的自動完成代碼。 輸入對我有用。

$(".tags").autocomplete({
        source: function(request, response) {
            $.getJSON("/actions.php?action=autocomplete", {
                term: extractLast(request.term)
            }, response);
    },
    search: function() {
            /* custom minLength */
            var term = extractLast(this.value);
            if (term.length < 1) {
                return false;
            }
    },
    focus: function() {
            /*prevent value inserted on focus */
            return false;
        },
        select: function(event, ui) {
            var terms = split( this.value );
            /* remove the current input*/
            terms.pop();
            /* add the selected item*/
            terms.push( ui.item.value );
            /* add placeholder to get the comma-and-space at the end*/
            terms.push("");
            this.value = terms.join(", ");
            return false;
        }
    });

對其進行修改,使其適合您的網址。 我正在使用jquery ui jQuery UI-v1.8.21

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM