使用 JavaScript 动态重命名表单元素
我在表单中有一个表格,其中填充了各种输入框。我编写了各种 JavaScript 函数,允许用户从表中添加或删除行,并且还会动态重命名用户输入该行之后的元素名称(如果这有意义 - 希望我的代码能够解释我正在尝试做什么)。
这适用于 IE9、Firefox 和 Chrome,但不适用于 IE7(我组织中的许多人目前仍在使用 XP)。
该代码不是我写过的最优雅的代码,但我需要在明天在人们面前演示它之前解决这个问题! (所以我愿意考虑任何事情,即使此时这是一个快速而肮脏的修复!)
谢谢!
for (var e = 0; e < editForm.elements.length; e++) {
// Increment 'add row' buttons so that they add underneath the correct row
if (editForm.elements[e].name.length >= 4) {
if (editForm.elements[e].name.substr(0, 4) == 'add_') {
stringPos = editForm.elements[e].name.indexOf('_');
currentQuestionNumber = editForm.elements[e].name.substr(stringPos + 1);
if (currentQuestionNumber > pos) {
editForm.elements[e].name = 'add_' + (parseInt(currentQuestionNumber) + 1);
editForm.elements[e].id = 'add_' + (parseInt(currentQuestionNumber) + 1);
editForm.elements[e].setAttribute("onclick", "addRow(" + (parseInt(currentQuestionNumber) + 1) + ");");
}
}
}
代替...
editForm.elements[e].name = 'add_' + (parseInt(currentQuestionNumber) + 1);
...我也使用了...
editForm.elements[e].setAttribute("name", "add_" + (parseInt(currentQuestionNumber) + 1));
...但这在IE7中也不起作用。任何帮助表示赞赏。
I have a table inside a form, which is populated with various input boxes. I have written various JavaScript functions that will allow the user to add or remove a row from the table, and will also dynamically rename the element names that come after where the user has entered the row (if that makes sense - hopefully my code will explain what I'm trying to do).
This works in IE9, Firefox and Chrome, but not in IE7 (which a lot of people in my organization who are still on XP currently use).
The code isn't the most elegant thing I've ever written, but I need to fix this before demoing it in front of people tomorrow! (So I'm willing to consider anything, even if it's a quick and dirty fix at this point!)
Thanks!
for (var e = 0; e < editForm.elements.length; e++) {
// Increment 'add row' buttons so that they add underneath the correct row
if (editForm.elements[e].name.length >= 4) {
if (editForm.elements[e].name.substr(0, 4) == 'add_') {
stringPos = editForm.elements[e].name.indexOf('_');
currentQuestionNumber = editForm.elements[e].name.substr(stringPos + 1);
if (currentQuestionNumber > pos) {
editForm.elements[e].name = 'add_' + (parseInt(currentQuestionNumber) + 1);
editForm.elements[e].id = 'add_' + (parseInt(currentQuestionNumber) + 1);
editForm.elements[e].setAttribute("onclick", "addRow(" + (parseInt(currentQuestionNumber) + 1) + ");");
}
}
}
In place of...
editForm.elements[e].name = 'add_' + (parseInt(currentQuestionNumber) + 1);
...I have also used...
editForm.elements[e].setAttribute("name", "add_" + (parseInt(currentQuestionNumber) + 1));
...but this doesn't work in IE7 either. Any help appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要更改名称的元素是动态创建的,则可能会遇到 IE6/7 错误。有一个指向 < 的可能解决方法的指针Web Bug Track 博客上的 code>setAttribute bug。您还可以找到“更改 jQuery 中克隆输入元素的名称属性在 IE6/7 中不起作用”的答案有用。
In case the elements you're changing names have been dynamically created, you may be hitting an IE6/7 bug. There's a pointer to a possible workaround to the
setAttribute
bug on the Web Bug Track blog. You may also find the answers to "Changing name attr of cloned input element in jQuery doesn't work in IE6/7" useful.