Javascript getElementsByName() 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6967297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:01:21  来源:igfitidea点击:

getElementsByName() not working?

javascripthtmlcodeignitergetelementsbyname

提问by sqlmole

I have a Javascript function which should update a hidden input field in my form with a number that increments every time the function is called.

我有一个 Javascript 函数,它应该使用每次调用函数时递增的数字更新表单中的隐藏输入字段。

It worked originally with getElementById()however because I had to redesign my form I cannot use the php function to assign an individual ID to the element so all I have is a unique name for that element.

它最初与getElementById() 一起使用,但是因为我必须重新设计我的表单,所以我无法使用 php 函数为元素分配一个单独的 ID,所以我只有该元素的唯一名称。

So instead I decided to use getElementsByName()from Javascript to modify the element.

因此,我决定使用Javascript 中的getElementsByName()来修改元素。

Here is the HTML of that element

这是该元素的 HTML

  <input type="hidden" value="" name="staff_counter">

This is my Javascript code:

这是我的 Javascript 代码:

window.onload=function()
{

//function is activated by a form button 

var staffbox = document.getElementsByName('staff_counter');
                    staffbox.value = s;


                s++;
}

I am getting no errors on Firebug when the function is called and the input field is not getting a value given to it.

当函数被调用并且输入字段没有获得给它的值时,我在 Firebug 上没有收到任何错误。

It was working with getElementById() but why all of a sudden it does not work with getElementsByName()?

它与 getElementById() 一起工作,但为什么突然间它不适用于 getElementsByName()?

  • -I have checked that it is the only unique element in the document.
  • -I checked for any errors on Firebug when activating the function
  • - 我已经检查过它是文档中唯一的唯一元素。
  • - 我在激活功能时检查了 Firebug 上的任何错误

Here is the code I use from Codeigniter to make the element

这是我从 Codeigniter 使用的代码来制作元素

// staff_counter is name and the set_value function sets the value from what is
//posted so if the validation fails and the page is reloaded the form element does
// not lose its value

echo form_hidden('staff_counter', set_value('staff_counter'));

Thanks

谢谢

回答by Digital Plane

document.getElementsByName()returns a NodeList, so you have to access it by an index: document.getElementsByName('staff_counter')[0](depending on how many of these you have).

document.getElementsByName()返回一个NodeList,因此您必须通过索引访问它:(document.getElementsByName('staff_counter')[0]取决于您拥有的索引数量)。

You also have access to a lengthproperty to check how many Elements were matched.

您还可以访问一个length属性来检查匹配了多少元素。

回答by Louis

Just had a similar issue, I resolved it with a simple loop over the array. The code below will go through and disable all the buttons with the name of 'btnSubmit'.

刚刚有一个类似的问题,我用一个简单的数组循环解决了它。下面的代码将通过并禁用所有名为“btnSubmit”的按钮。

for (var i=0;i<document.getElementsByName('btnSubmit').length;i++){
    document.getElementsByName('btnSubmit')[i].disabled=true;
}