onchange 不起作用
我正在通过 O'Reilly 媒体的 Head First 系列书学习 JavaScript,刚刚到达必须使用 onchange
事件的章节。
我正在使用 Safari/OS X Lion、Firefox、Chrome、Opera 和 IE/Windows 进行测试,但得到了相同的结果。
给出这段代码:
<html>
<head>
<title>onChange Test</title>
<script type="text/javascript">
function itWorks(){
alert("it works!");
}
</script>
</head>
<body>
<form>
<input type="text" onchange="itWorks();" />
<input type="text" onchange="itWorks();" />
</form>
</body>
<html>
只要我们从一个字段更改为另一个字段,无论仅通过单击还是使用 TAB 键激活该事件,onchange
事件都会起作用,这样说是否正确?
I am learning JavaScript through the Head First series book by O'Reilly media, and I just reached a chapter where I have to use the onchange
event.
I'm testing using Safari/OS X Lion, Firefox, Chrome, Opera, and IE/Windows, but got the same result.
Given this code:
<html>
<head>
<title>onChange Test</title>
<script type="text/javascript">
function itWorks(){
alert("it works!");
}
</script>
</head>
<body>
<form>
<input type="text" onchange="itWorks();" />
<input type="text" onchange="itWorks();" />
</form>
</body>
<html>
Is it correct to say that the onchange
event works whenever we change from one field to another, whether it is activated only by clicking or by using the TAB key?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
onchange 事件在以下情况下触发:
焦点如何丢失并不重要,并且焦点不需要移动到另一个字段(链接可以是重点,或者文档中没有任何内容,等等)。
The onchange event fires when:
It doesn't matter how focus was lost, and focus doesn't need to move to another field (a link could be focused, or nothing in the document could be, etc).
这就是 onblur。
只要您更改输入的值,然后离开该字段,您编写的事件就会触发。例如:在字段中输入一些内容,然后按 TAB 键。
您的示例代码按预期工作我。
Thats onblur.
the event you have coded fires whenever you change the value of the input, then leave the field. EG: Enter something into the field, then press the TAB key.
Your example code works as expected for me.
您所描述的行为是焦点。 onchange 当输入值更改时执行。
如果您在字段中输入一些内容,它应该会运行。
The behaviors you described is onfocus. onchange executes when the value of the input changes.
If you type something into the field, it should run.
是的 - 只要值发生了变化,
我不确定问题是什么 - 你的代码可以工作!
我在上测试了 jsfiddle.net - 这对于学习/测试 javascript 非常有用。
(顺便说一句,你应该关闭你的 html 标签)...
Yes - as long as the value has changed
I'm not sure what the question is tbh - your code works!
I tested it on jsfiddle.net - which is great for learing / testing javascript.
(you should close your html tag btw)...
这是 HTML5 草案规范的内容:
请注意,更改也可以在其他时间触发。 规范还指出 :
并继续提供几个“做出改变”的例子
This is what the HTML5 draft spec says:
Note that change can fire at other times too. The spec also says:
And goes on to provide a couple of examples of "committing a change"