onchange 不起作用

发布于 2024-12-18 17:02:39 字数 769 浏览 4 评论 0原文

我正在通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦里的微风 2024-12-25 17:02:39

onchange 事件在以下情况下触发:

  • 焦点离开字段
  • 如果自获得焦点以来值发生了变化

焦点如何丢失并不重要,并且焦点不需要移动到另一个字段(链接可以是重点,或者文档中没有任何内容,等等)。

The onchange event fires when:

  • Focus leaves the field
  • if the value has changed since focus was gained

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).

雪花飘飘的天空 2024-12-25 17:02:39

“我们从一个字段更改为另一个字段,无论是通过单击还是通过
使用 TAB 键”-

这就是 onblur。

只要您更改输入的值,然后离开该字段,您编写的事件就会触发。例如:在字段中输入一些内容,然后按 TAB 键。

您的示例代码按预期工作我。

"we change from one field to another, whether its by clicking or by
using the TAB key" -

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.

梦境 2024-12-25 17:02:39

您所描述的行为是焦点。 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.

猫七 2024-12-25 17:02:39

“澄清一下,每当我们从一个字段更改为另一个字段时,无论是通过单击还是使用 TAB 键,onchange 事件都会起作用,对吗?”

是的 - 只要值发生了变化,

我不确定问题是什么 - 你的代码可以工作!
上测试了 jsfiddle.net - 这对于学习/测试 javascript 非常有用。

(顺便说一句,你应该关闭你的 html 标签)...

"Just to clarify, the onchange event works whenever we change from one field to another, whether its by clicking or by using the TAB key, right?"

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)...

本宫微胖 2024-12-25 17:02:39

这是 HTML5 草案规范的内容

失焦步骤如下:

如果元素是输入元素,并且更改事件适用
到元素,并且该元素没有定义的激活
行为,并且用户更改了元素的值或其列表
在控件聚焦时选择文件而不提交
更改,然后触发一个名为 change 的简单事件
元素。

取消元素的焦点。

在元素上触发一个名为“blur”的简单事件。

请注意,更改也可以在其他时间触发。 规范还指出 :

...任何时候用户提交对元素值或列表的更改
选定的文件后,用户代理必须将任务排队以触发简单的任务
在输入元素处冒泡名为 change 的事件。

并继续提供几个“做出改变”的例子

具有提交操作的用户界面的示例是文件
上传控件由一个打开文件的按钮组成
选择对话框:当对话框关闭时,如果文件
结果选择发生了变化,然后用户提交了一个新文件
选择。

具有提交操作的用户界面的另一个示例是
允许基于文本的用户输入和用户选择的日期控件
从下拉日历:虽然文本输入可能没有明确的
提交步骤,从下拉日历中选择一个日期,然后
取消下拉列表将是一个提交操作。

This is what the HTML5 draft spec says:

The unfocusing steps are as follows:

If the element is an input element, and the change event applies
to the element, and the element does not have a defined activation
behavior, and the user has changed the element's value or its list of
selected files while the control was focused without committing that
change, then fire a simple event that bubbles named change at the
element.

Unfocus the element.

Fire a simple event named blur at the element.

Note that change can fire at other times too. The spec also says:

... any time the user commits a change to the element's value or list of
selected files, the user agent must queue a task to fire a simple
event that bubbles named change at the input element.

And goes on to provide a couple of examples of "committing a change"

An example of a user interface with a commit action would be a File
Upload control that consists of a single button that brings up a file
selection dialog: when the dialog is closed, if that the file
selection changed as a result, then the user has committed a new file
selection.

Another example of a user interface with a commit action would be a
Date control that allows both text-based user input and user selection
from a drop-down calendar: while text input might not have an explicit
commit step, selecting a date from the drop down calendar and then
dismissing the drop down would be a commit action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文