Here is my javascript code, when I clcik the button, if 开发者_运维百科no set is chosen and no check box is marked, the alert window should show both the messages, but at the moment it only show the first one which is "Please check all the box", what am I doing wrong, thanks!
$('#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;
}
});
You need to append to the fail
variable with +=
rather than overwriting/reassigning it:
// Before anything else, initialize:
var fail = "";
// Later...
// First error:
fail += "Please chooose a set.\n";
// Later still...
fail += "Please check all the box.\n";
Because you assign to fail
both times instead of concatenating strings (with a "\n" in the middle so they're on different lines).
You might want to practice "playing computer", walking through your code line-by-line, writing down exactly what's happening at every step.
I'm also not sure that your loop dealing with flag
will do what you intend, but to be honest, I find your code a little hard to follow.
精彩评论