0

「都市」入力フィールドで jQuery オートコンプリート インスタンス (v1.1) をデバッグしています。

オートコンプリート データ バインディングはkeyup、フィールド値の長さが少なくとも 2 で、値が変更された場合にのみ、フィールドのイベントで設定されます。

データ バインディング、ヒント リスト、およびオートコンプリートは正常に機能しています。しかし、何かが間違っています。ログを見ると、サーバーに投稿された都市の入力値の 3 番目と 4 番目の文字が欠落していることが何度もあります。いくつかの例:

"trpani" instead of "trapani"
"fienze" instead of "firenze"
"scndicci" instead of "scandicci"
"brdisi" instead of "brindisi"
"paermo" instead of "palermo"
"caliari" instead of "cagliari"

バグを再現することはできませんが、偶然ではないと確信しています! エラーは 3 文字目で発生します。挿入時に、Javascript の処理がキーボード バッファと衝突するようです。

だから、私は推測しますが、よくわかりませf i r e n z eん.rfienze

質問

何が起こっている?エラーを再現できないのはなぜですか? なにが問題ですか?

オートコンプリートを設定するために使用されるコードは次のとおりです。

/*
 * here I store the previous value
 */
var storeCity;
$('#City').keydown(function(){
    var val = $(this).val();
    storeCity = (val.length >=2 ? val.substring(0,2):"");
});

/*
 * the autocomplete is set only if
 * - the City value has changed compared to the storeCity value
 * - the City value has lenght at least 2
 * - remark: using the keypress does not solve the issue
 */
$('#City').keyup(function(){
    var val = $(this).val();
    if(val.length>=2 && val.substring(0,2)!=storeCity){
        getLocations('#City');
    }
});

function getLocations(cityId){
    var hint = $(cityId).val().substring(0,2);

    $.ajax({
        type: "GET",
        url: "/include/getLocations.asp",
        data: ({location : hint}),
        async: false,
        error: function() {},
        success: function(dataget) {
            var result = dataget.split("\n");

            $(cityId).flushCache();
            $(cityId).autocomplete(result, {
                pleft: 100px,
                minChars: 2,
                matchContains: true,
                max: 3000,
                delay: 100,
                formatItem: function(row){...},
                formatMatch: function(row){ return row[0]; },
                formatResult: function(row){ return row[0]; }
            });

            $(cityId).result(function(event,data){...});
        }
    });
}
4

2 に答える 2

0

初期化の順序が逆になるようです。オートコンプリート コンポーネントの初期化は、ページの読み込み時に 1 回だけ行う必要があります。添付の例を参照してください:

  $( "#city" ).autocomplete({
        source: function( request, response ) {
           // Put your ajax request here
        }
  });
于 2012-11-19T21:18:15.650 に答える
0

最終的に、いくつかのヒューリスティックで解決しました。問題はイベントの競合によるものだと思いました。だから、パフォーマンスを上げようとした。

  • getLocation() メソッドの外で、オートコンプリートを一度初期化します
  • $('#city') オブジェクトを変数に格納する
  • $city.result メソッドを getLocation() メソッドの外に置く

結果コードは次のとおりです。

var $city = $('#City');
var storeCity;

$city.keypress(function(){
    var val = $(this).val();
    storeCity = (val.length >=2 ? val.substring(0,2):"");
});

/*
 * the autocomplete is set only if
 * - the City value has changed compared to the storeCity value
 * - the City value has lenght at least 2
 * - remark: using the keypress does not solve the issue
 */
$city.keyup(function(){
    var val = $(this).val();
    if(val.length>=2 && val.substring(0,2)!=storeCity){
        getLocations('#' + $city[0].id);
    }
});

$city.autocomplete({
    pleft: 100px,
    minChars: 2,
    matchContains: true,
    max: 3000,
    delay: 50
});

$city.result(function(event,data){...});

function getLocations(cityId){
    var hint = $(cityId).val().substring(0,2);

    $.ajax({
        type: "GET",
        url: "/include/getLocations.asp",
        data: ({location : hint}),
        async: false,
        error: function() {...},
        success: function(dataget) {
            var result = dataget.split("\n");

            $(cityId).flushCache();
            $(cityId).autocomplete(result, {
                formatItem: function(row){...},
                formatMatch: function(row){ return row[0]; },
                formatResult: function(row){ return row[0]; }
            });
        }
    });
}
于 2012-11-29T11:25:48.383 に答える