如何使用 Javascript 中的变量动态构造文档元素字符串?
这让我抓狂,我确信这是可能的,而且做起来肯定很简单。
我有一个页面,上面有一大堆动态创建的表单。在我的一个函数中,我需要访问这些表单之一,因此我将表单的名称传递到变量中。
然后我需要使用文档树访问该表单的名称。
但是,当我输入变量时,它假定变量的名称是表单的名称。
所以这不起作用:
function myAwesomeFunction(nameOfForm)
{
var selection = document.nameOfForm.nameOfInput.selectedIndex;
}
所以我环顾网络,发现我需要使用括号表示法,但这也不起作用:
function myAwesomeFunction(nameOfForm)
{
var selection = document[nameOfForm].nameOfInput.selectedIndex;
}
我还尝试了一些引用操作:
function myAwesomeFunction(nameOfForm)
{
var selection = document['nameOfForm'].nameOfInput.selectedIndex;
}
...但没有喜悦。
那么,我哪里出错了?
对于奖励积分...如果表单的名称和特定输入的名称都是动态的怎么办?然后呢?
function myAwesomeFunction(nameOfForm, nameOfInput)
{
var selection = document[nameOfForm][nameOfInput].selectedIndex;
}
This is driving me nuts, and I'm sure it's both possible and surely simple to do.
I have a page with a whole bunch of dynamically created forms on it. In one of my functions, I need to access one of those forms, so I pass the name of the form in a variable.
Then I need to access the name of that form using the document tree.
However, when I put in the variable, it assumes the name of the variable is the name of the form.
So this does not work:
function myAwesomeFunction(nameOfForm)
{
var selection = document.nameOfForm.nameOfInput.selectedIndex;
}
So I looked around the net and saw that I need to use bracket notation, but this doesn't work either:
function myAwesomeFunction(nameOfForm)
{
var selection = document[nameOfForm].nameOfInput.selectedIndex;
}
I also tried with some quotation action:
function myAwesomeFunction(nameOfForm)
{
var selection = document['nameOfForm'].nameOfInput.selectedIndex;
}
... but no joy.
So, where am I going wrong?
For bonus points... what if both the name of the form and the name of the particular input were both dynamic? Then what?
function myAwesomeFunction(nameOfForm, nameOfInput)
{
var selection = document[nameOfForm][nameOfInput].selectedIndex;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在表单对象中查找它们- 这不起作用,因为它是一个数组而不是一个对象。使用
document.getElementsByName
甚至更好,在表单上设置 id 属性并使用
document.getElementById
查找表单Look them up in the forms object- this won't work since it is an array and not an object.use
document.getElementsByName
or even better, set an id attribuite on the form and use
document.getElementById
to find the form尝试使用
document.getElementById(nameOfForm)
(如果表单上也有 ID)...如果您可以在页面中包含 jQuery 引用,则可以轻松执行以下操作(再次假设您在表格上有 ID):
Try using
document.getElementById(nameOfForm)
(if you have the ID on the form as well)...If you can include a jQuery reference to your page, you can easily do the following (again assuming you have the ID on the form):
函数 focusElement(formName, elemName) {
var elem = document.forms[formName].elements[elemName];
}
试试这个
formname 是表单名称,elemname 是输入标签名称
function focusElement(formName, elemName) {
var elem = document.forms[formName].elements[elemName];
}
try this
formname is name of the form and elemname is input label name