0

我有一个隐藏的 div,其中 id 为“res”,如果设置了 cookie,我会回显用户名,否则它会回显 nouser

我在头上有这个代码来检查用户是否存在打开一个fancybox来购买产品,否则他将被要求注册

$("a.add_to_cart").click(function (e) {
    e.preventDefault();

    var res = $("div#res").text();
    if (res == "nouser") {
        noty({
            "text": "you must login first",
            "layout": "center",
            "type": "error",
            "textAlign": "center",
            "easing": "swing",
            "animateOpen": {
                "height": "toggle"
            },
            "modal": "true",
            "animateClose": {
                "height": "toggle"
            },
            "speed": "500",
            "timeout": "2000",
            "closable": true,
            onClose: function () {},
            "closeOnSelfClick": true
        });
    } else {
        var pro_name = $(this).attr("pro_name");
        var pro_id = $(this).attr("id");
        var qst = "?pro=" + pro_id;
        var ajax = false;
        ajax = new XMLHttpRequest();
        ajax.open("get", "ajax/check_exists.php" + qst);
        ajax.onreadystatechange = function () {
            if (ajax.readyState == 4 && ajax.status == 200) {
                var result = ajax.responseText;
                if (result == 'in cart') {

                    noty({
                        "text": "product already exists in the cart",
                        "layout": "center",
                        "type": "error",
                        "textAlign": "center",
                        "easing": "swing",
                        "animateOpen": {
                            "height": "toggle"
                        },
                        "modal": "true",
                        "animateClose": {
                            "height": "toggle"
                        },
                        "speed": "500",
                        "timeout": "2000",
                        "closable": true,
                        onClose: function () {},
                        "closeOnSelfClick": true
                    });


                } else {
                    $("a.add_to_cart[id=" + pro_id + "]").unbind("click");
                    var qst1 = "?pro=" + pro_id;
                    $("#proID").text(pro_id);
                    var ajax1 = false;
                    ajax1 = new XMLHttpRequest();
                    ajax1.open("get", "ajax/get_submit.php" + qst1);
                    ajax1.onreadystatechange = function () {
                        if (ajax1.readyState == 4 && ajax1.status == 200) {
                            $("#addDiv").html(ajax1.responseText);
                        }
                    }
                    ajax1.send(null);

                    $("a[id=" + pro_id + "]").fancybox({
                        'transitionIn': 'elastic',
                            'transitionOut': 'elastic'
                    }).click();
                }
            }
        }
        ajax.send(null);


    }

});

当我使用 firebug 时,我发现 div 包含: nouser 但fancybox 不断显示 jquery if 条件有什么问题?

4

1 回答 1

1

您的值中是否有任何额外的空格.text()?也许尝试在条件之前修剪值if(res.trim() == 'nouser')

于 2013-05-29T16:58:24.977 回答