繁体   English   中英

警报未完全显示

[英]alert doesn't show completely

这是我的JavaScript代码,单击按钮时,如果未选择任何设置且未选中复选框,则警报窗口应同时显示两条消息,但目前仅显示第一个消息,即“请检查所有盒子”,我在做什么错,谢谢!

$('#movetoset').click(function() {
    var fail = "";

    if ($('#selectsett').val() === 'General') {

        fail = "Please chooose a set.\n";
    }

    for (j = 0; j < array_str_idnum.length; j++) {
        if (document.getElementById('check' + array_str_idnum[j]).checked) {
            document.getElementById('imagediv' + array_str_idnum[j]).style.display = 'none';
            //  (Add to database here?)
            array_str_idnum[j] = 'flag_gone'; // for example              
        }


        if (document.getElementById('check' + array_str_idnum[j]).checked == false) {
            fail = "Please check all the box.\n";
        }

    }
    flag = false;

    for (j = 0; j < array_str_idnum.length; j++) {

        if (array_str_idnum[j] == 'flag_gone') {
            flag = flag && true;
        }
        else {
            flag = false
        }

    }
    if (flag == true) {
        $('#steptwo').hide();
        $('#begin').fadeIn();
    }

    if (fail == "") {
        return true;
    } else {
        alert(fail);
        return false;
    }
});

您需要使用+=附加到fail变量,而不是覆盖/重新分配它:

// Before anything else, initialize:
var fail = "";

// Later...
// First error:
fail += "Please chooose a set.\n";

// Later still... 
fail += "Please check all the box.\n";

因为您两次都分配fail ,而不是串联字符串(中间带有“ \\ n”,所以它们位于不同的行)。

您可能想练习“玩电脑”,逐行浏览代码, 准确记录每一步发生的情况。

我也不确定您处理flag的循环是否会达到您的预期,但是说实话,我发现您的代码有点难以理解。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM