0

このコードの if ステートメントが常に true を返す理由を説明してください。

(function ($) {
    //load on page load
    $(".area").load("/galleries/ #projects > li a");

    //load on widget title click
    $('.widget-top').live("click", function () {
        $(".area").load("/galleries/ #projects > li a");
    });

    //stop default href from working
    $('.area a').unbind().live("click", function () {
        event.preventDefault();
        return;
    });

    // variables
    var i = $('#widgets-right .addinput').size() + 1;
    var addDiv = $('.addinput');
    //dynamic forms



    $('#widgets-right .addNew').live('click', function () {
        $(this).parents('.addinput').append('<div class="div_' + i + '"><h4>Reference project ' + i + '</h4><div class="gallery_one_image_wrap"><img class="gallery_one" src="" /><br/></div><p><label for="title' + i + '">Title:</label><input type="text" class="title' + i + '" name="title' + i + '" value="title' + i + '" style="width:100%;" /></p><p><label for="name' + i + '">The link:</label><input type="text" class="link' + i + '" id="name' + i + '" name="name' + i + '" value="name' + i + '" style="width:100%;" /></p><p><label for="img' + i + '">The Link to the image:</label><input type="text" class="img' + i + '" id="img' + i + '" name="img' + i + '" value="img' + i + '" style="width:100%;" /></p><a href="#" class="remNew">Remove</a></div>');
        i++;
        alert(i);
        return false;
    });

    $('#widgets-right .remNew').live('click', function () {
        if (i > 1) {
            $(this).parent('div').remove();
            i--;
            alert(i);
        }
        return false;
    });

    //load into input boxes
    if (i === 2) {

        $("#widgets-right .area a").live("click", function () {
            alert(i);
            alert("this is the if ");
            var title = $(this).attr('title');
            $(".title").val(title);
            var link = $(this).attr('href');
            $(".link").val(link);
            var img = $("img", this).attr('src');
            $(".img").val(img);
            var imgexample = $("img", this).attr('src');
            $(".gallery_one").attr("src", imgexample);

        });
    } else {
        alert("this is the else!");
    }


}(jQuery));

問題の if/else ステートメントは次のとおりです。

//load into input boxes
if ( i === 2){

$("#widgets-right .area a").live("click", function() {
  alert(i);
alert("this is the if ");
          var title = $(this).attr('title');
          $(".title").val(title);
          var link = $(this).attr('href');
          $(".link").val(link);
          var img = $("img", this).attr('src');
          $(".img").val(img);
          var imgexample = $("img", this).attr('src');
          $(".gallery_one").attr("src", imgexample);

    });
}else{   
alert("this is the else!")
}

クリックするたび#widgets-right .area aに、i が 2 を超えていても、常に if に移動します。なぜですか? クリス

4

2 に答える 2

9

イベントハンドラーのif に入れるつもりだと思います:

$("#widgets-right .area a").live("click", function () {
    if (i === 2) {

現在の方法では、チェックはページが読み込まれたときに 1 回だけ実行されます。

于 2013-07-17T21:07:42.890 に答える
1

ステートメントifはクリック ハンドラーに含まれていないため、1 回だけ実行されます。したがって、i別のハンドラーのどこかでインクリメントしている場合でも、クリック ハンドラーはアタッチされたままです。

ページが読み込まれるたびにクリックハンドラーをアタッチし、iそのハンドラー内の値を確認したいと思います。

$(document).on('#widgets-right .area a', 'click', function() {
    if(i === 2) {
        alert('This is the if!');
    } else {
        alert(' This is the else!');
    }
});
于 2013-07-17T21:07:30.220 に答える