Javascript IndexOf 不适用于表单数组

发布于 2024-11-07 18:05:23 字数 352 浏览 3 评论 0原文

为什么控制台会出现以下情况?或者我滥用了indexOf?

document.forms:

[
<form id=​"form-0" name=​"form-0">​…​</form>​
, 
<form id=​"form-1" name=​"form-1">​…​</form>​
, 
<form id=​"form-2" name=​"form-2">​…​</form>​
]

document.forms.indexOf["form-0"]:

TypeError: Cannot read property 'form-0' of undefined

How come the following happens in the console? or am i misusing the indexOf?

document.forms:

[
<form id=​"form-0" name=​"form-0">​…​</form>​
, 
<form id=​"form-1" name=​"form-1">​…​</form>​
, 
<form id=​"form-2" name=​"form-2">​…​</form>​
]

document.forms.indexOf["form-0"]:

TypeError: Cannot read property 'form-0' of undefined

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

嘿嘿嘿 2024-11-14 18:05:23

Document.forms 是一个集合。如果您想要表格的编号(如您的评论中所示),那么问题仍然存在:您在哪一刻想要该编号?无论如何,您可以创建一个表单数组:

var allforms = document.getElementsByTagName('form'), 
    formsArray = [];
for (var i=0;i<allforms.length;i++){
   if (allforms[i].id.match(/\d+$/)){
       var indexval = parseInt(allforms[i].id.replace(/(.+)(\d+)$/,'$2'),10);
       formsArray[indexval] = allforms[i];
   }
}

现在您有一个包含对所有表单的引用的数组,并且对于每个表单,都有一个索引值,该索引值反映您通过其 id 为其提供的表单编号。因此: formsArray[0] 包含对 forms['form-0'] 的引用,formsArray[1]forms[ 'form-1']

Document.forms is a collection. If you want the number of a form - as indicated in your comments -, the question remains: at which moment do you want that number? Anyway, you could create an array of forms:

var allforms = document.getElementsByTagName('form'), 
    formsArray = [];
for (var i=0;i<allforms.length;i++){
   if (allforms[i].id.match(/\d+$/)){
       var indexval = parseInt(allforms[i].id.replace(/(.+)(\d+)$/,'$2'),10);
       formsArray[indexval] = allforms[i];
   }
}

Now you have an Array containing references to all forms, and for each form an index value that reflects the form number you gave it via it's id. So: formsArray[0] contains a reference to forms['form-0'], formsArray[1] to forms['form-1'] etc.

月竹挽风 2024-11-14 18:05:23

您使用了错误的语法...indexOf 是一种方法,因此它应该使用括号,而不是方括号。

someString.indexOf("form-0")

对于对象,您可以简单地使用括号请求该对象:

document.forms["form-0"]

You've used the wrong syntax... indexOf is a method, so it should use parenthesis, not brackets.

someString.indexOf("form-0")

With an object, you can simply request the object using brackets:

document.forms["form-0"]
对岸观火 2024-11-14 18:05:23

document.forms 是一个 HTMLCollection。它不是一个数组。因此,它没有 indexOf 方法。

您可以使用 Array.prototype.slice.call 对其进行转换:

var formsArr = Array.prototype.slice.call(document.forms);

但是请注意,并不普遍支持 indexOf。然而,我不完全确定你想要做什么,所以无论如何,这很可能不是必要的方法。

document.forms is an HTMLCollection. It is not an array. As such, it does not have an indexOf method.

You can convert it using Array.prototype.slice.call:

var formsArr = Array.prototype.slice.call(document.forms);

Note, however, that indexOf is not universally supported. I'm not entirely sure what you're trying to do, however, so it may well not be the necessary approach anyway.

筱果果 2024-11-14 18:05:23

如果您尝试通过 ID 获取元素,只需直接使用 ID 即可。

document.forms['form-0'];

如果你想要它的索引,看起来你已经在 ID 中拥有它了。只需删除 "form-" 部分即可。

var index = 'form-0'.replace('form-','');

或者,如果您有该元素,请通过其 ID 属性执行相同操作:

var myform = document.forms['form-0'];
var index = myform.id.replace('form-','')

If you're trying to get the element by its ID, just use the ID directly.

document.forms['form-0'];

If you want its index, it would seem that you already have it in the ID. Just remove the "form-" part.

var index = 'form-0'.replace('form-','');

Or if you have the element, do the same via its ID property:

var myform = document.forms['form-0'];
var index = myform.id.replace('form-','')
笑饮青盏花 2024-11-14 18:05:23

如果您想使用 javascript 访问表单的元素,那么您必须使用:document.myform.elements[i],其中 i 是元素的位置。您可以参考这个Javascript表单对象 - 属性和方法教程。 indexof 是 javascript 字符串对象的方法。

If you want to access the elements of a form using javascript, then you have to use this: document.myform.elements[i], where i is the position of the element. You may refer this Javascript Form Objects - Properties and Methods tutorial. indexof is a method of javascript string object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文