[英]Can't get my simple JavaScript Event Handler working
I am trying to get an event handler on an HTML form.我正在尝试在 HTML 表单上获取事件处理程序。 I am just trying t get the simplest thing working, but I just cannot see what I am missing.
我只是想让最简单的事情工作,但我就是看不到我错过了什么。
It is part of a wider project, but since I cannot get this bit working I have reduced it down the most very basic elements 1 text field and a button to try and see what it is I am missing.它是一个更广泛的项目的一部分,但由于我无法让这部分工作,我将它减少了最基本的元素 1 文本字段和一个按钮,以尝试查看我缺少的内容。
All I want to do is get some text entered and flash up message in a different area on the screen.我想要做的就是在屏幕上的不同区域输入一些文本并闪现消息。
The user enters text into the input field (id=owner).用户在输入字段中输入文本 (id=owner)。 The plan is that when the button (id="entry") is pressed the event handler (function "entry") in the entry.js file should cause a message to display.
计划是当按下按钮 (id="entry") 时,entry.js 文件中的事件处理程序(函数“entry”)应该会显示一条消息。
I Know that this is beginner stuff & I know that I have reduced this down such that it barely worth thought but I would welcome any input please & thank you.我知道这是初学者的东西,我知道我已经把它减少了,以至于几乎不值得思考,但我欢迎任何输入,谢谢。
<form method="post" action="">
<label for="owner">Input Owner: </label>
<input type="text" id="owner" />
<div id="feedback"></div>
<input type="submit" value="enter" id="entry" />
</form>
<script src="entry.js"></script>
function entry() {
var elOwner = document.getElementById('owner');
var elMsg = document.getElementByID('feedback');
elMsg.textContent = 'hello';
}
var elEntry = document.getElementById('entry');
elEntry.onsubmit=entry;
Adding in a prevent default:添加防止默认值:
window.event.preventDefault(); window.event.preventDefault();
doing this through an event Listener:通过事件侦听器执行此操作:
elEntry.addEventListener('submit',entry,false); elEntry.addEventListener('submit',entry,false);
using innerHTML to post the message:使用 innerHTML 发布消息:
elMsg.innerHTML = " elMsg.innerHTML = "
At present all that happens is that the pushing submit reloads the page - with no indication of any text being posted anywhere.目前发生的所有事情是推送提交重新加载页面 - 没有任何文本被张贴在任何地方的迹象。
One issue is that you have a typo, where getElementById
capitalized the D
at the end.一个问题是你有一个错字,
getElementById
在末尾将D
大写。
Another is that preventDefault()
should be called on the form
element, not the input.另一个是应该在
form
元素而不是输入上preventDefault()
。
Here's a working example that corrects those two mistakes.这是一个纠正这两个错误的工作示例。
function entry(event) { var elOwner = document.getElementById('owner'); var elMsg = document.getElementById('feedback'); elMsg.textContent = 'hello'; event.preventDefault(); } var entryForm = document.getElementById('entry').form; entryForm.onsubmit = entry;
<form method="post" action=""> <label for="owner">Input Owner: </label> <input type="text" id="owner" /> <div id="feedback"></div> <input type="submit" value="enter" id="entry" /> </form>
I also defined a event
parameter for the handler.我还为处理程序定义了一个
event
参数。 I don't remember is window.event
was ever standardized (it probably was), but I'd prefer the parameter.我不记得
window.event
是否曾经标准化(可能是),但我更喜欢参数。
Be sure to keep your developer console open so that you can get information on errors that may result from typos.请确保您的开发者控制台保持打开状态,以便您可以获得有关可能因拼写错误而导致的错误的信息。
var elEntry = document.getElementById('entry'); elEntry.addEventListener("click", function(e) { e.preventDefault(); var elMsg = document.getElementById('feedback'); elMsg.textContent = 'hello'; });
<form method="post" action=""> <label for="owner">Input Owner: </label> <input type="text" id="owner" /> <div id="feedback"></div> <input type="submit" value="enter" id="entry" /> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.