好吧,这是我最初的问题。你不必阅读它,但如果它有帮助:Firefox认为<;fieldset>;是一个表单元素;Chrome则不然
基本上,Firefox和IE将HTML中的字段集视为数组中的一个元素,这就把一切都搞砸了。但是Google Chrome并不将字段集视为数组元素。如果浏览器计算字段集,我试图通过一种方式设置新数组来解决这个问题,如果计算了字段集,则通过另一种方式进行设置。这是我的密码。我认为问题出在if语句上。
var $ = function (id) { return document.getElementById(id); }
function check() {
var x = $("myForm");
var user = new Array();
var type = x.elements[0].type;
if (x.elements[0].nodeName=="fieldset") {
user[0] = x.elements[1].value;
user[1] = x.elements[3].value;
user[2] = x.elements[5].value;
user[3] = x.elements[2].value;
user[4] = x.elements[4].value;
user[5] = x.elements[6].value;
} else {
user[0] = x.elements[0].value;
user[1] = x.elements[2].value;
user[2] = x.elements[4].value;
user[3] = x.elements[1].value;
user[4] = x.elements[3].value;
user[5] = x.elements[5].value;
}
var answers = new Array();
answers[0] = "sample1";
answers[1] = "sample2";
answers[2] = "sample3";
answers[3] = "sample4";
answers[4] = "sample5";
answers[5] = "sample6";
var display = new Array();
for (var i=0;i<6;i++) {
if (user[i] == "") {
display[i] = "You entered nothing.";
}
else if (user[i] == answers[i]) {
display[i] = "Correct!";
}
else {
display[i] = "Wrong. The correct answer is "" + answers[i] + "".";
}
}
alert(display[0] + "
" + display[1] + "
" + display[2] + "
" + display[3] + "
" + display[4] + "
" + display[5]);
}