我有以下 html:
<input type="text" id="theInput" />
<input id="clickMe" type="button" value="Search" onclick="testFunction(document.getElementById('theInput').value)" /><br>
我正在尝试触发这个:
function testFunction(data)
{
alert(data);
}
我还尝试了另一种方法,使用:
document.getElementById("clickMe").onclick = testFunction(document.getElementById("theInput").value);
遗憾的是,这两种方法似乎都没有触发函数。我确定我遗漏了一些简单的东西,有人能指出来吗?
最佳答案
我测试了一下,你的代码没问题,可以看到demo here .但是,由于您包含 javascript 函数的方式,我认为它在您的页面中不起作用。最好将所有脚本放在页面的head
标签中。
如果您使用某些框架,请确保您的函数和绑定(bind)在所有 javascript 加载时执行。
关于单击按钮不会触发 JavaScript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17778941/