0

入力テキストの包含に変数である部分文字列が含まれているかどうかを確認しようとしています:これが私のコードです:

<script>
    $("#envoi_search").click(function () {

        var contenu = $("#champ").val() ;
        $("#envoi_search").click(function() {
            $("#result").html('') ;
            $('#envoi_search').attr("disabled", "disabled");
            $.ajax({
                type: "POST",
                url: "http://www.edumobile.org/blog/uploads/XML-parsing-data/Data.xml",
                data: "{}",
                cache: false,
                dataType: "xml",
                success: function(data) {
                    $(data).find("Book").each(function () {
                        var temp = $(this).find("name").text() ;
                        if (temp.toLowerCase().contenu > -1) {
                            $("#result").append("<br> Titre : " + temp);
                            $("#result").append("<br> Auteur : " + $(this).find("address").text());
                            $("#result").append("<br> Pays : " + $(this).find("country").text());
                            $('#envoi_search').attr("disabled", "");
                        }
                    });

                }
            });
        });
    });
</script>

そして、なぜそれが機能しないのかわかりません。私の問題について何か考えがありますか?ありがとう :)

4

1 に答える 1

0
//buffer element since we use it multiple times
var $envoi_search = $("#envoi_search");

//bind a single click event handler to the element
$envoi_search.click(function () {

    //create a regular expression based on the value of a form input
    var myRegExp = new RegExp($("#champ").val(), "gi");

    $("#result").html('');

    $envoi_search.attr("disabled", "disabled");

    $.ajax({
        type     : "POST",
        url      : "http://www.edumobile.org/blog/uploads/XML-parsing-data/Data.xml",
        cache    : false,
        dataType : "xml",
        success  : function(data) {
            $(data).find("Book").each(function () {
                var temp = $(this).find("name").text();

                            //use `.search()` to test the regular expression against a string
                if (temp.toLowerCase().search(myRegExp) > -1) {

                                    //it's a waste to do multiple selections and appends in a row, instead just append everything at once
                    $("#result").append("<br> Titre : " + temp + "<br> Auteur : " + $(this).find("address").text() + "<br> Pays : " + $(this).find("country").text());
                    $envoi_search.attr("disabled", "");
                }
            });

        }
    });
});

これがあなたが探しているものだと思います。内部イベント バインディングを削除しました.click。これは基本的に、同じイベントのイベント ハンドラー内でイベント バインディングをセットアップすることでした。これにより、内部イベント ハンドラーを機能させるには 2 回クリックする必要がありました。

また、同じドメインにいない場合はwww.edumobile.org、サーバー側の言語でプロキシ スクリプトを記述して、クロスドメイン ポリシーの問題なしにデータを取得できるようにする必要があります。

.search()文字列内の文字列の存在をチェックしていたときに、関数がありませんでした。

var myRegExp = new RegExp($("#champ").val(), "gi");

#champこれにより、入力の値に基づいて正規表現が作成されます。"gi"フラグは次のとおりです。

g-> グローバル (実行しているので実際には必要ありません.search()が、これは非常に便利です)

i-> 大文字と小文字を区別しない

あなたのためのいくつかのドキュメント:

.search(): https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/search RegExp() : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp `

于 2012-05-09T21:37:42.833 に答える