0

#txtAllowSearch がフラット html である限り機能する次のスクリプトがあります。

$(document).ready(function(){
    $("#txtAllowSearch").autocomplete({
        source: "test_array.aspx",
        delay: 0,
        select: function (event, ui) {
            $("#txtAllowSearch").val(ui.item.value); // display the selected text
            $("#txtAllowSearchID").val(ui.item.id); // save selected id to hidden input
        }
    });
});

#txtAllowSearch が javascript/jquery によって動的に作成されるとすぐに、これは機能しなくなります。

これを機能させるにはjqueriesをライブで使用する必要がありますか?

4

2 に答える 2

3

jQuerys .live()help //イベントをキャッチするのに.delegate()役立ちます。あなたの場合 (要素にプラグイン メソッドを適用する)、要素が DOM に挿入されるたびに呼び出すか、優れたヘルププラグインを使用する必要があります。.autocomplete().livequery

于 2011-01-31T09:33:10.610 に答える
2

jQuery.live は非推奨になりました。

これを実現するには、$(document).on を使用する必要があります。

$(document).ready(function(){
    $(document).on("focus.autocomplete", "#txtAllowSearch", function() {
        source: "test_array.aspx",
        delay: 0,
        select: function (event, ui) {
            $("#txtAllowSearch").val(ui.item.value); // display the selected text
            $("#txtAllowSearchID").val(ui.item.id); // save selected id to hidden input
        }
    });
});

詳細については、jQuery API ドキュメントを参照してください: http://api.jquery.com/on/

于 2012-11-08T00:41:33.430 に答える