0

プロジェクトで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 値全体が現在選択されている値に変更されます。それを修正する方法?

ありがとう 、

4

1 に答える 1

1

これは私のオートコンプリート コードです。入力してください。

$(".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;
        }
    });

URL に合うように変更します。jquery ui jQuery UI - v1.8.21 を使用しています

于 2012-12-04T11:22:14.763 に答える