1

検索スクリプトであるjQueryスクリプトがありますが、ウィンドウのサイズが変更されたときに要素のサイズを(画面の高さ-40px)に変更する機能も含まれています。ただし、検索(AJAXクエリ)がアクティブなときにサイズ変更機能を無効にしたい。誰かが私がこれを行う方法を知っていますか?

私の現在のコードは次のとおりです。

$(document).ready(function(){
    $(window).resize(function(){
        if($(window).height()<1200){
            $("#a").height($(window).height()-40);
        }
    });
    $("form").submit(function(a){
        a.preventDefault();
        if($("#i").val().length>0){
            $.ajax({
                type:"get",
                url:"search.php?q="+q,
                dataType:"html",
                success:function(a){
                    ...
                }
            })
        }
    })
})
4

3 に答える 3

1

.on() と .off() を使用する

$(document).ready(function(){
    function started(){
        if($(window).height()<1200){
            $("#a").height($(window).height()-40);
        }
    $(window).on("resize.name_space",started);

    $("form").submit(function(a){
        a.preventDefault();
        if($("#i").val().length>0){
           $(window).off("resize.name_space");
            $.ajax({
                type:"get",
                url:"search.php?q="+q,
                dataType:"html",
                success:function(a){
                $(window).on("resize.name_space",started);    ...
                }
            })
        }
    })
})
于 2012-07-19T09:28:12.953 に答える
0
var loading;
...
    if($(window).height()<1200 && !loading){
...
        loading = true;
        $.ajax({
            ...
            complete: function(){
                loading = false;
            }.
        })
    }
})
于 2012-07-19T09:27:59.113 に答える
0

これを試してみてください。ajaxの実行中にチェックインが追加され、サイズ変更が行われなくなります

$(document).ready(function(){

//Create the variable
var check = false;

$(window).resize(function(){

    //Is the ajax currently running? This if statement runs if the answer is no
    if (check == false && $(window).height()<1200 ) {
        $("#a").height($(window).height()-40);
    }

});
$("form").submit(function(a){
    a.preventDefault();

    //Set the variable to true, this stops the resizing above ^^
    check = true;
    if($("#i").val().length>0){
        $.ajax({
            type:"get",
            url:"search.php?q="+q,
            dataType:"html",
            success:function(a){
                ...
            },
            complete: function() {
                //Set back to off once the ajax has finished
                check = false;
            }
        })
    }
})

}))

于 2012-07-19T09:27:28.153 に答える