0

このコードのほとんどは、jQueryUIドキュメントから取得しました。<input>IDも送信されるように、リクエスト変数に追加したいと思います。

入力は次のようになります。

<input size="50" class="attr_values" name="msrp" id="msrp" />

これがjqueryです。.autocompleteソース$.getJSONの内部で「this.id」を使用しようとしましたが、機能しません。どうすればこれを機能させることができますか?

$(document).ready(function() { 
    // 2 string parsing functions
    function split( val ) {
        return val.split( /,\s*/ );
    }
    function extractLast( term ) {
        return split( term ).pop();
    }


    $( '.attr_values' )
        // don't navigate away from the field on tab when selecting an item
        .bind( 'keydown', function( event ) {
            if ( event.keyCode === $.ui.keyCode.TAB &&
                    $( this ).data( 'autocomplete' ).menu.active ) {
                event.preventDefault();
            }
        })
        .autocomplete({
            source: function( request, response ) {
                $.getJSON( 'controllers/core_data/ajax/core_data.php', {
                    'attr_name' : this.id, // THIS DOESN'T WORK
                    'term': extractLast( request.term )
                }, response );
            },
            search: function() {
                // custom minLength
                var term = extractLast( this.value );
                if ( term.length < 2 ) {
                    return false;
                }
                // var attr_name = this.id;
            },
            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;
            }
        });

 });

また、例が示すように1つの特定のIDではなく、「。attr_values」クラスにこれを適用したことにも注意してください。私にはたくさんのフィールドがあり、それぞれが異なるIDを持っています。そのため、idAND用語をphpスクリプトに送信する必要があります。そのため、どのテーブルで用語を検索するかがわかります。

4

2 に答える 2

1

thisコールバック内で元のコレクションを参照しなくなりましたgetJSON。元の要素に到達するために上向きにドリルしてみてください。

source: function( request, response ) {
    $.getJSON( 'controllers/core_data/ajax/core_data.php', {
        'attr_name' : this.element[0].id,
        'term': extractLast( request.term )
    }, response );
},

ここでのコンテキストは、関数を評価しているオブジェクトをthis指します。プロパティを使用して対応するjQueryコレクションを取得し、そこからIDを見つけることができます。autocompletesourceelement

于 2012-12-18T06:58:42.297 に答える
-1

これを与えてみてください。

$(this).attr('id');
于 2012-12-18T06:59:25.480 に答える