我还没有在网上找到答案,调试人员也没有告诉我足够的理解我可以使用的替代品。 是否可以使用数组将“填充”值发布到文本框中以实现我在下面尝试使用的效果?我可以编写document.form.a.value = fill; document.form.b.value = fill;等等,但循环和数组似乎更简单和有效。 这是代码的简化版本,它给了我一个错误:
<script type="text/javascript">
function postData() {
var fill = 0.0;
var txtBoxLetter = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
for (var i = 0; i < txtBoxLetter.length; i++) {
document.form.txtBoxLetter[i].value = fill; // Error on this line
}
}
</script>
<form name="form" method="" action="">
<input type="text" name="a" size="1" readonly="true">
<input type="text" name="b" size="1" readonly="true">
<input type="text" name="c" size="1" readonly="true">
<input type="text" name="d" size="1" readonly="true">
<input type="text" name="e" size="1" readonly="true">
<input type="text" name="f" size="1" readonly="true">
<input type="text" name="g" size="1" readonly="true">
<input type="text" name="h" size="1" readonly="true">
// etc.
</form>
感谢您的帮助!
答案 0 :(得分:2)
使用方括号将变量用作属性名称...
document.forms[0][txtBoxLetter[i]].value = fill;
当你这样做时,你正在做同样的事情......
txtBoxLetter[i]
如果你真的有a-z,你可以跳过数组......
for (var i = 0; i < 26; i++) {
document.forms[0][String.fromCharCode(i + 97)].value = fill;
}
答案 1 :(得分:2)
document.form.elements[ txtBoxLetter[i] ].value = fill;