0

私のタイトルは少し分かりにくいかもしれません。私はjqueryオートコンプリートで次のことをしようとしています:

このフィドルを参照してください:

http://jsfiddle.net/96Y2y/

ユーザーが「j」と入力すると、Jochen + John が表示されます。ここまでは順調ですね。彼がジョンを選んだとしましょう。私が知りたいのは、彼がジョンを選択した後、角かっこ内の数字を除いて、入力のすべてがカットされるということです。結果は次のようになります。

7、

このアクションの後、ユーザーはカンマの後に再び名前を書き始めることができるはずです。たとえば、「chris」と書いたとしましょう。選択後、入力は次のようになります。

7、9

等々。これは可能ですか?

最初のステップは、「onfinishedselect」アクションを確認することだと思います

select: function (event, ui){
alert("|" + $("#targetID").val() + "|1stAlert");
}

この投稿のように: jQuery UI autocomplete fire new event after selected item

よろしくお願いします。助けてくれてありがとう。

トニー

4

2 に答える 2

1

複数の値のデモのコードを使用して、括弧の間に数値のみを挿入する正規表現を使用できます。

function extractLast( term ) {
    return split( term ).pop();
}

function split( val ) {
    return val.split( /,\s*/ );
}

$(function() {
    var availableTags = [
        "Max (1)", 
        "Paul (3)",
        "John (7)",
        "Jochen (11)",
        "Chris (9)"
    ];
    $( "#tags" ) .autocomplete({
        minLength: 0,
        source: function( request, response ) {
            // We trim the search term before comparing to the source
            request.term = $.trim(request.term);
            // delegate back to autocomplete, but extract the last term
            response( $.ui.autocomplete.filter(
                availableTags, extractLast( request.term ) ) );
        },
        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 number between parentheses for the selected item
            var num = ui.item.value.match(/\((\d+)\)/);
            var value = num[1];
            terms.push( value );
            // add placeholder to get the comma-and-space at the end
            terms.push( "" );
            this.value = terms.join( "," );
            return false;
        }
    });
});

ここで jsFiddle を参照してください: http://jsfiddle.net/96Y2y/2/

于 2013-02-15T09:54:30.347 に答える
0

おそらく最善の解決策は、オートコンプリートではない別のフィールドに数値を入力することです。

このプラグインの「onItemSelect」機能でそれを行うことができますhttps://github.com/dyve/jquery-autocomplete

于 2013-02-15T09:28:23.400 に答える