<分区>
我有一个 Javascript 函数,它应该使用每次调用该函数时递增的数字更新我表单中的隐藏输入字段。
它最初使用 getElementById() 但是因为我必须重新设计我的表单我不能使用 php 函数为元素分配一个单独的 ID 所以我所拥有的只是该元素的唯一名称.
因此我决定使用 Javascript 中的 getElementsByName() 来修改元素。
这是该元素的 HTML
<input type="hidden" value="" name="staff_counter">
这是我的 Javascript 代码:
window.onload=function()
{
//function is activated by a form button
var staffbox = document.getElementsByName('staff_counter');
staffbox.value = s;
s++;
}
调用该函数时,我在 Firebug 上没有收到任何错误,并且输入字段未获得赋予它的值。
它与 getElementById() 一起工作,但为什么突然间它不能与 getElementsByName() 一起工作?
- -我已检查它是文档中唯一的唯一元素。
- -我在激活该功能时检查了 Firebug 是否有任何错误
这是我使用 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'));
谢谢