0

ページに動的に作成される入力フィールドがいくつかあります。JQueryでは、値をphpスクリプトに渡してオートコンプリートに入力できるように、入力している値を取得しようとしています。

例えば:

 <input id="inventory_location_1">
 <input id="inventory_location_2">
 <input id="inventory_location_3">
 <input id="inventory_location_4">
 <input id="inventory_location_5">
 <input id="inventory_location_6">

何らかの理由で「inventory_location_1」にテキストを入力している場合、var strValue = $(this).val(); を使用できません。入力しているボックスのテキストを取得します。Jquery はエラーをスローします。

これが私の完全な Jquery スクリプトです。

 $(function() {
        $('input[id^=inventory_location_]').autocomplete({
            source: function( request, response ) {
                $("#progressBar").show();
                var strValue = $(this).val();
                alert(asdf);
                $.ajax({
                    url: '{{ path('inventory_id_via_ajax_array') }}',
                    dataType: "json",
                    data: {
                        featureClass: "P",
                        style: "full",
                        maxRows: 12,
                        value: strValue
                    },
                    success: function( data ) {
                        if(data.responseCount <= 0){
                        }
                        response( $.map( data.responseData, function( item ) {
                            return {
                                label: item.name,
                                name: item.name,
                                value: item.name,
                                id: item.id
                            }
                        }));
                        $("#progressBar").hide();
                    }
                });
            },
            minLength: 2,
            select: function( event, ui ) {
                $("#progressBar").hide();
                $(this).val(ui.item.label);
            },
            open: function() {
                $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
            },
            close: function() {
                $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
            }
        });
    });

どうすればいいのかわからないので、考えられることはすべてやった。また、私のスクリプトに正しくない点が他にもある場合は、アドバイスしてください。ご協力いただきありがとうございます!!!

4

2 に答える 2

1

ページ内の多くの要素でプラグインを初期化するが、特定の各要素にアクセスする必要がある場合にうまく機能する簡単な回避策を次に示します。

 $('input[id^=inventory_location_]').each(function(){
           /* within $.each  "this" is the current element*/
           var ID=this.id; // can now pass  variable into plugin wherever you need it
             $(this).autocomplete({/* options*/});

});
于 2013-10-28T22:24:18.603 に答える
0

参照している行は関数内にあるため、「this」キーワードは入力フィールドに関連しなくなりました。

また、オートコンプリート ソース関数内から他のタスクを実行しようとしているように見えますが、これは適切な場所ではないと思います。

于 2013-10-28T22:30:02.403 に答える