如果其他javascript语句总是返回如果?

vim*_*984 0 javascript jquery if-statement

有人可以解释为什么这段代码中的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));
Run Code Online (Sandbox Code Playgroud)

有问题的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!")
}
Run Code Online (Sandbox Code Playgroud)

每当我点击#widgets-right .area a它总是会转到if,即使i高于2.为什么?克里斯

McG*_*gle 9

我认为你的意思是把事件处理程序放在if 内部:

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

现在的方式是,当页面加载时,检查只会执行一次.