English 中文(简体)
JavaScript:我有一个数组。我想检查第一个元素是否是字段集
原标题:JavaScript: I have an array. I want to check if the first element is a fieldset

好吧,这是我最初的问题。你不必阅读它,但如果它有帮助: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]);
}
最佳回答

您遇到的问题主要是因为nodeName通常返回大写文本(取决于浏览器),而您将其与小写字段集进行比较。为了使其一致,您应该使用字符串stoLowerCase函数。

关于条件:我认为更优雅的方法是创建一个新的filtered数组,而不使用<;字段集>和提交按钮。

最后,重新创建数组的方式涉及到有点太多的键入。

我建议您使用用于创建数组对象的文字表示法

演示]单击预览

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

var filtered = [];
var elements = $("myForm").elements;
var len = elements.length;

// opt out fieldsets and submit buttons
for (var i = 0; i < len; i++) {
  var el = elements[i];
  if (el.nodeName.toLowerCase() != "fieldset" && el.type != "submit") {
    filtered.push(el);
  }
}

// specify an order (of indices)
var user = [1, 3, 5, 2, 4, 6];

// then replace numbers with element 
// values  from the filtered array
for (var i = 0; i < user.length; i++) {
  user[i] = filtered[ user[i] ].value;
}
问题回答

暂无回答




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!