为什么`document.getElementById(“#datepicker1”)`找不到我的元素? [英] Why does `document.getElementById(“#datepicker1”)` not find my element?
本文介绍了为什么`document.getElementById(“#datepicker1”)`找不到我的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图根据下拉列表的选择显示/隐藏文本输入。
I am trying to show / hide a text input based on the selection of a drop down list.
这是一个 JSFiddle 到我想做的事情。
Here’s a JSFiddle to what I am trying to do.
进入该函数时,得到一个 TypeError
:无法设置 null
的属性' type
。
When I get into the function, I get a "TypeError
: Cannot set property 'type
' of null
".
这是代码:
function showCustomDate(val) {
console.log(val);
if (val == 4) {
var y = document.getElementById("#datepicker1");
//console.log(y.type);
y.type = "text";
}
else {
var y = document.getElementById("#datepicker1");
//console.log(y.type);
y.type = "hidden";
}
};
推荐答案
y
为空,因为元素的ID可能是 datepicker1
而不是#datepicker1
y
is null, because the id of the element is probably datepicker1
and not #datepicker1
var y = document.getElementById("#datepicker1");
应该是
var y = document.getElementById("datepicker1");
这篇关于为什么`document.getElementById(“#datepicker1”)`找不到我的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文