我想一定形式的getElementById,但它显示错误的getElementById在Javascript
遗漏的类型错误:对象#有没有一种方法 '的getElementById'
这里是我的代码
varForm = document.forms['newform'].getElementById("yellow");
我想一定形式的getElementById,但它显示错误的getElementById在Javascript
遗漏的类型错误:对象#有没有一种方法 '的getElementById'
这里是我的代码
varForm = document.forms['newform'].getElementById("yellow");
一个ID是唯一的 - 所以没有区别(你得到你想要的),直接调用它(/正确):
var Form = document.getElementById("yellow");
var Form = document.getElementById("yellow");
这会得到你的表格。
考虑这种形式:
<form id="myform"><input id="lala" type="text" value="123"/></form>
有几种方法获得 “拉拉” 输入的值:
console.log(document.getElementById("lala").value)
console.log(document.forms[0].lala.value)
console.log(document.forms["myform"].lala.value)
console.log(document.forms.myform.lala.value)
这里是Fiddle一起玩。
使用以下方法从另一种形式获取输入值,例如
形式
<form id="form1">
<input id="txt1" type="text" value="111"/>
</form>
<form id="form2">
<input id="txt1" type="text" value="222"/>
</form>
的js
//get input value from current form i.e. form 1
alert(document.getElementById("txt1").value) //111
//get input value from other form i.e. form2
alert(document.forms["form2"].txt1.value) //222
正如其他人所指出的那样,只要ID是唯一的,我们有getElementById
在document
:
varForm = document.getElementById("yellow");
但是,如果你仍然坚持查找基于特定dom节点的节点时,可以尝试:
varForm = document.forms['newform'].querySelector("#yellow");
Dom节点没有getElementById,只能在document(document.getElementById)上使用。如果你需要类似的东西尝试使用.querySelector(“#黄色”)或.querySelectorAll(“。黄色”) –