奇怪的javascript问题,如果语句test = true但不会执行?

时间:2014-08-25 18:52:07

标签: javascript jquery

在此点击处理程序代码中:

$('#save').click(function () {
            if (overAllStatus == 'red') {
                rows[lastRowClicked].cells[0].innerHTML = '<a class="modalInput" rel="#flagsSummary" style="cursor:pointer"><img src="/FatcaOne_0/static/images/circleRed.png" width="20" height="20"></a>';
            }
            else if (overAllStatus == "yellow") {
                rows[lastRowClicked].cells[0].innerHTML = '<a class="modalInput" rel="#flagsSummary" style="cursor:pointer"><img src="/FatcaOne_0/static/images/circleYellow.png" width="20" height="20"></a>';
                console.log('Over all status is yellow.');
            }
            else if (overAllStatus == 'greenR') {
                rows[lastRowClicked].cells[0].innerHTML = '<a class="modalInput" rel="#flagsSummary" style="cursor:pointer"><img src="/FatcaOne_0/static/images/circleGreenHollow.png" width="20" height="20"></a>';
            }
            else if (overAllStatus == 'greenN') {
                rows[lastRowClicked].cells[0].innerHTML = '<a class="modalInput" rel="#flagsSummary" style="cursor:pointer"><img src="/FatcaOne_0/static/images/circleGreen.png" width="20" height="20"></a>';
            }
            comments = $('#comments').val();
            $("body").trigger(esc);
            $("#save1").trigger('click');
            redListSize = 0;
            yellowListSize = 0;
            console.log("save clicked");
        });

当在JS控制台中测试变量overAllStatus的值为yellow时,我在单击“保存”按钮之前在控制台中看到了这一点:

> overAllStatus
"yellow"
> overAllStatus == 'yellow'
true

我只在控制台中看到“保存已点击”。看起来测试条件(overAllStatus == "yellow")中的代码不会执行。我不明白为什么。

1 个答案:

答案 0 :(得分:3)

尝试用这个代替你的代码,看看“overAllStatus”在你的“点击”回调中有什么价值

$('#save').click(function () {
            console.log("overAllStatus", overAllStatus);  //use this to check the value of overAllStatus

            if (overAllStatus == 'red') {
                rows[lastRowClicked].cells[0].innerHTML = '<a class="modalInput" rel="#flagsSummary" style="cursor:pointer"><img src="/FatcaOne_0/static/images/circleRed.png" width="20" height="20"></a>';
            }
            else if (overAllStatus == "yellow") {
                rows[lastRowClicked].cells[0].innerHTML = '<a class="modalInput" rel="#flagsSummary" style="cursor:pointer"><img src="/FatcaOne_0/static/images/circleYellow.png" width="20" height="20"></a>';
                console.log('Over all status is yellow.');
            }
            else if (overAllStatus == 'greenR') {
                rows[lastRowClicked].cells[0].innerHTML = '<a class="modalInput" rel="#flagsSummary" style="cursor:pointer"><img src="/FatcaOne_0/static/images/circleGreenHollow.png" width="20" height="20"></a>';
            }
            else if (overAllStatus == 'greenN') {
                rows[lastRowClicked].cells[0].innerHTML = '<a class="modalInput" rel="#flagsSummary" style="cursor:pointer"><img src="/FatcaOne_0/static/images/circleGreen.png" width="20" height="20"></a>';
            }
            comments = $('#comments').val();
            $("body").trigger(esc);
            $("#save1").trigger('click');
            redListSize = 0;
            yellowListSize = 0;
            console.log("save clicked");
        });