Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
if-else javascript语句始终返回if?_Javascript_Jquery_If Statement - Fatal编程技术网

if-else javascript语句始终返回if?

if-else javascript语句始终返回if?,javascript,jquery,if-statement,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/ #pro

有人能解释一下为什么这段代码中的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));
每当我点击右边的widgets.area时,它总是转到if,即使i大于2。 为什么?
Chris

您的
if
语句只运行一次,因为它不包含在click处理程序中。因此,即使您在另一个处理程序中的某个位置递增
i
,您的单击处理程序仍然是附加的

我猜您希望在每次加载页面时附加click处理程序,然后在该处理程序中检查
I
的值:

$(document).on('#widgets-right .area a', 'click', function() {
    if(i === 2) {
        alert('This is the if!');
    } else {
        alert(' This is the else!');
    }
});
$("#widgets-right .area a").live("click", function () {
    if (i === 2) {

我想您的意思是将
if
放在事件处理程序中:

$(document).on('#widgets-right .area a', 'click', function() {
    if(i === 2) {
        alert('This is the if!');
    } else {
        alert(' This is the else!');
    }
});
$("#widgets-right .area a").live("click", function () {
    if (i === 2) {

按照现在的方式,当页面加载时,检查只执行一次。

Live在1.7中被弃用,在1.9中被删除。开始使用on()来简化将来的升级。我想您希望在
click
处理程序中包含
if
语句。我知道。live不推荐使用。为什么if语句…
.size()
也被弃用。改用
.length
。@Stephan你搞错了。他应该使用
===
而不是
=
。这个问题每天都会出现(条件语句包装绑定,而不是相反)。就是这样!!!你是天才,我是修女!谢谢,这两天很长,我犯了一个愚蠢的错误。