为什么这个 javascript 自动填充多个字段不起作用?
我是 JavaScript 新手,我不明白为什么这段代码不起作用。我想让用户在元输入表中输入数据,并让他们单击 [input] 以在每列的所有行中输入数据,例如,如果将 12/22/2012 设置为出生并单击输入,对于男性和女性表,“出生”下的所有字段都应填写。我将html代码从页面复制到jsfiddle中, http://jsfiddle.net/hopup/ h2jjd/3/
感谢任何帮助,请记住我是 JavaScript 新手,所以建设性的批评会很好。
这是 Javascript
编辑 2:对我的代码做了一些更改,我是否更接近了?它仍然不起作用。
编辑3:这适用于 Chrome,但不适用于 Firefox。有什么想法吗?
http://jsfiddle.net/hopup/h2jjd/5/ <-- 检查这个一
function fill (colname) {
var numRows, i, toCopy, iterated_name;
numRows = document.getElementById('malesTable').rows.length + document.getElementById('femalesTable').rows.length - 2;
toCopy = document.getElementById(colname.id).value;
i = 1;
for (i; i <= numRows; i++){
iterated_name = colname.id + "_" + i;
document.getElementById(iterated_name).value = toCopy;
}
}
I'm new to JavaScript and I can't figure out why this code isn't working. I want to have the user input data at the meta input table and have them click [input] to input the data in all of the rows for each column, for instance if 12/22/2012 is put for born and input is clicked, all fields under 'born' should be filled for both male and female tables. I copied the code for the html from the page into a jsfiddle, http://jsfiddle.net/hopup/h2jjd/3/
Any help appreciated, please remember I'm new to JavaScript so constructive criticism would be nice.
here is the Javascript
EDIT 2: Made some changes to my code, am I getting closer? It still does not work.
EDIT 3:THis works on chrome, but not on firefox. Any thoughts?
http://jsfiddle.net/hopup/h2jjd/5/ <-- check this one
function fill (colname) {
var numRows, i, toCopy, iterated_name;
numRows = document.getElementById('malesTable').rows.length + document.getElementById('femalesTable').rows.length - 2;
toCopy = document.getElementById(colname.id).value;
i = 1;
for (i; i <= numRows; i++){
iterated_name = colname.id + "_" + i;
document.getElementById(iterated_name).value = toCopy;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这条线
似乎很奇怪。我发现了三个错误:
document.myform.elements[count]
getElement/s/ByName
方法,它返回元素的 NodeList。您可能想使用 ids,getElementById()
只返回具有唯一 id 的一个元素。The line
seems to be the odd one. Three errors I've spotted:
document.myform.elements[count]
getElement/s/ByName
method, which returns a NodeList of elements. You might want to use ids, there isgetElementById()
to return only the one element with the unique id.您的 JavaScript 源文件中存在非法字符。
编辑看起来像一个非西方字形,U+8B80 - 读您在问题中发布的版本不一样,至少不完全一样。
编辑啊durrr,它是UTF-8,当然不是UTF-16。正如@Pumbaa80 指出的那样,这是一个零宽度的空间。哦,具体来说,它位于文件 mausdb.js 中。
There are illegal characters in your JavaScript source file.
edit looks like a non-Western glyph, U+8B80 - 讀 The version you posted in the question is not the same, at least not exactly.
edit ah durrr it's UTF-8, not UTF-16 of course. It's a zero-width space, as @Pumbaa80 points out. Oh, and specifically this is in the file mausdb.js.