Firefox认为<fieldset>是一个表单元素; Chrome没有</fieldset>

时间:2010-10-22 21:53:21

标签: javascript html firefox google-chrome

在我的JavaScript程序中,我通过其元素从表单中获取值,然后在警报中按顺序打印它们。这在Firefox中运行良好,但在Chrome中,订单很古怪,并以“提交”按钮结束。

我试图摆脱字段集并调整数字,但它确实有效,但我喜欢这个字段集!此外,我不能只是制作一个数组并迭代它,因为字段是按Tab键顺序调整的,我想相应地打印它们。有什么建议吗?

在尝试验证时,我发现我确实需要XHTML Strict的字段集。我一直在将元素存储在数组中,如下所示:

var $ = function (id) { return document.getElementById(id); }

function check() {
var x = $("myForm");

var user = new Array();
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] + "\n" + display[1] + "\n" + display[2] + "\n" + display[3] + "\n" + display[4] + "\n" + display[5]);
}

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

我认为你最好使用ID:

<form ...>
  <input ... id="q0" />
  <input ... id="q1" />
  <input ... id="q2" />
</form>

所以你可以写这个JavaScript代码:

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++) {
  var user = $('q' + i).value;
  if (user == "")
    display[i] = "You entered nothing.";
  else if (user == answers[i])
    display[i] = "Correct!";
  else
    display[i] = "Wrong. The correct answer is \"" + answers[i] + "\".";
}
alert(display[0] + "\n" + display[1] + "\n" + display[2] + "\n" + display[3] + "\n" + display[4] + "\n" + display[5]);

您的代码也可以获得很多改进:

var answers = [ "sample1", "sample2", "sample3", "sample4", "sample5", "sample6" ];

var display = new Array();
for (var i=0;i<6;i++) {
  var user = $('q' + i).value;
  if (user == "")
    display.push( "You entered nothing." );
  else if (user == answers[i])
    display.push( "Correct!" );
  else
    display.push ( "Wrong. The correct answer is \"" + answers[i] + "\"." );
}
alert(display.join('\n'));