简体   繁体   English

JavaScript:我有一个数组。 我想检查第一个元素是否为字段集

[英]JavaScript: I have an array. I want to check if the first element is a fieldset

OK, so here was my original problem. 好的,这是我最初的问题。 You don't have to read it but in case it helps: Firefox thinks <fieldset> is a form element; 您不必阅读它,但万一它有帮助: Firefox认为<fieldset>是一个表单元素; Chrome doesn't Chrome没有

Basically, Firefox and IE count the fieldset in my HTML as an element in my array, and that screws everything up. 基本上,Firefox和IE会将HTML中的字段集视为数组中的一个元素,这会使所有事情搞砸了。 But Google Chrome does not count the fieldset as an array element. 但是Google Chrome浏览器不将字段集视为数组元素。 I'm trying to solve the problem by setting the new array one way if the browser counts the fieldset, and setting it a different way if it does. 我试图通过以下方法解决问题:浏览器计算字段集时以一种方式设置新数组,如果这样做则设置为另一种方式。 Here's my code. 这是我的代码。 I think the problem is with the if statement. 我认为问题在于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] + "\n" + display[1] + "\n" + display[2] + "\n" + display[3] + "\n" + display[4] + "\n" + display[5]);
}

The problem you were having is mainly because nodeName usually returns uppercase text (depending on the browser), and you are comparing it with lowercase fieldset . 您遇到的问题主要是因为nodeName 通常返回大写文本(取决于浏览器),并且您将其与小写的fieldset进行比较。 To make it consistent you should use string's toLowerCase function. 为了使其一致,您应该使用字符串的toLowerCase函数。

About the conditionals: I think a more elegant way would be to create a new filtered array without <fieldset> and submit button. 关于条件:我认为一种更优雅的方法是创建一个没有<fieldset>并提交按钮的新filtered数组。

Finally, the way you're creating your arrays involves a little bit too much typing. 最后,创建数组的方式涉及太多的输入。

I suggest you using literal notation for creating Arrays and Objects . 我建议您使用文字表示法创建ArraysObjects

[ Demo ] ( click preview ) [ 演示 ]单击预览

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;
}

暂无
暂无

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

相关问题 我有一个包含图像和文档的对象数组,我想检查 mime_type 并选择要显示的第一个元素(React)<img> 标签 - I have a array of objects containing images and documents, I want to check the mime_type and pick the first element (React) to display in <img> tag 我在将 function 添加到数组时遇到问题。 (JavaScript) - I have an issue with adding a function to an array. (JavaScript) 我有数据存储在Perl数组中。 如何使用JSON将它们转换为JavaScript数组? - I have data stored in Perl array. How do I convert them into JavaScript array using JSON? 使用indexOf比较数组的第一个和下一个元素。 JavaScript的 - using indexOf to compare first and next element of array. JavaScript Javascript对象数组。 它继续覆盖第一个元素 - Javascript object array. It's keep on over writing the first element 我有两个数组。 我想以一种可以从另一个数组中获取最接近的值的方式比较它们 - I have two array. I want to compare both of them in a way that I can get the nearest value from the other array Javascript 如何优化大阵列中的延迟。 它有 350000+ 项 - Javascript how can i do optimize the delay in big array. its have 350000+ item 我有一个JavaScript数组,我想下载数组中的每个图像 - I have a JavaScript array and I want to download each image in the array 我想在以下 javascript 数组中的 details obj 中添加一个名为 Averagetimespent 的新变量。 有人可以帮我解决这个问题吗? - I want to add a new variable called Averagetimespent in the details obj inside the following javascript array. Could anyone help me out with this? 我在API响应中收到一个对象数组,并且是嵌套数组。 我希望通过ID找到一个对象 - I'm receiving an array of object in the API response and is nested array. i want find a object by ID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM