简体   繁体   English

HTML:如何在HTML中创建真正的“ onchange”事件(无jquery)?

[英]HTML : How to make a real “onchange” event in HTML (no jquery)?

please note: I do not want to use jQuery for this (otherwise I like it) 请注意:我不想为此使用jQuery (否则我喜欢它)

Problem: I have come to situation where I need to do some javascript function after an user changes an input field (eg input type="text"). 问题:我遇到一种情况,在用户更改输入字段(例如,输入type =“ text”)之后,我需要执行一些javascript函数。 So I said to myself - I remember an onchange event - BUT onchange event runs AFTER user leaves the field, but I need the function to be called for example every time user types a character to that field. 所以我对自己说-我记得一个onchange事件- 但是 onchange事件是在用户离开该字段后运行的,但是例如,每次用户在该字段中键入字符时,我都需要调用该函数。 Wowhead shows nice example of what I am trying to achieve this (field with placeholder "Search within results...") Wowhead的节目我试图达到很好的例子 (与占位场“在结果中搜索...”)

Summary: I am looking for a SIMPLE way of detecting REAL onchange event( not the classic HTML one )and through that call a JS function while not using jQuery ? 摘要:我正在寻找一种检测REAL onchange事件的简单方法( 不是经典的HTML方法 ),并通过一种在不使用jQuery调用JS函数的方法?

Thanks in advance ! 提前致谢 ! :) :)

然后使用onkeyup而不是onchange。

Following is a simple way of invoking each type of Key Press on field. 以下是在字段上调用每种类型的按键的简单方法。

input type="text" onkeypress="myFunction() 输入type =“ text” onkeypress =“ myFunction()

Get Example here 在此处获取示例

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onkeypress http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onkeypress

Enjoy.. 请享用..

you can also use onkeyup and onkeydown instead of onkeypress. 您也可以使用onkeyup和onkeydown代替onkeypress。

Tried onkeypress="myFunction()" or onkeyup="myFunction()"? 尝试过onkeypress =“ myFunction()”或onkeyup =“ myFunction()”吗?

There are also events for onfocus and onblur for entering and leaving a textfield :) 还有用于onfocus和onblur的事件,用于输入和离开文本字段:)

You should use "input" event. 您应该使用“输入”事件。 keyup and keypress don't work if a user modified the value only by a mouse. 如果用户仅通过鼠标修改了该值,则keyup和keypress不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM