提问者:小点点

未捕获的TypeError:undefined不是函数-不是JQuery


对于提交另一个未捕获的TypeError我感到很糟糕:未定义不是一个函数问题。但是,在阅读了大约20种不同的答案后,我不是错过了重点,就是问题在这些情况下是不同的。

我有一个有两个单选按钮的表单。单击一个时,我希望它更改标记的innerHTML。

这是我的小提琴。

HTML:

<form name="myForm" autocomplete="off" action="index.php" method="post" enctype="multipart/form-data">
<label for="mode">Text</label>
<input type="radio" name="mode" value="text" id="setText" />
<label for="mode">Image</label>
<input type="radio" name="mode" value="img" id="setImg" />
<table>
<tr id="headerInput"></tr>
</table>
</form>

JavaScript:

document.getElementById("setText").addEventListener("click", function () {
window.getElementById("headerInput").innerHTML = '<td>Title</td><td><input type="text" name="title" value="Hello" /></td>';
});
document.getElementById("setImg").addEventListener("click", function () {
window.getElementById("headerInput").innerHTML = '<td>Banner</td><td><input type="file" name="banner" /></td>';
});

我尝试了几个不同的版本,比如使用onclick=“someFunction()”属性和object.onclick创建命名函数。它们都给了我同样的错误。非常感谢您帮助我了解我正在做的事情。


共2个答案

匿名用户

Window.GetElementById()在浏览器中未正常定义。

窗口上查找属性“Get ElementById”返回未定义。然后尝试调用undefined,因此未捕获的TypeError:undefined不是函数。

您可能是想在那里改用document

匿名用户

您应该引用document.getElementById()而不是window.getElementById()。此外,更改innerHTML也是可以的,但如果要进行任何操作,我们应该参考tr中的td。这是最好的做法。

document.getElementById("headerInput").innerHTML

工作小提琴:http://jsfidle.net/7fpbg/