[英]What event to know when user has entered data into form?
I have a HTML form. 我有一个HTML表单。 I want to enable/disable a button until user eneters text in one of the fields. 我想启用/禁用按钮,直到用户在其中一个字段中输入文字。 I am adding an event attribute to the which triggers some javascript. 我向触发了一些JavaScript的事件属性。 This javascript will enable/disable the button. 此javascript将启用/禁用按钮。 Problem is I can't figure out what event attribute to use. 问题是我不知道要使用哪个事件属性。 What event attribute please will trigger as soon as user enters data? 用户输入数据后,将触发什么事件属性? I tried onchange but that only gets called when i clicked back outside the text area. 我尝试了onchange,但是只有在我在文本区域之外单击时才调用它。 So it may aswell be onblur. 因此它可能是模糊的。
You can use the input
您可以使用input
function activateForm (event) {
if(!this.value == ""){
}
}
var input = document.querySelector(".myInput");
input.addEventListener("input", activateForm , false)
There are 2 possible events that can be used: either onChange
or onKeyPress
. 可以使用两种可能的事件: onChange
或onKeyPress
。 onChange
will trigger when the value of an input has changed while onKeyPress
will trigger every time the user types something in a text box. 当输入值更改时, onChange
将触发,而每次用户在文本框中键入内容时, onKeyPress
将触发。 The onChange triggers once the user has CHANGED something in the value, and got out of the input focus. 一旦用户更改了值中的某些内容并脱离了输入焦点,onChange就会触发。 That means the user has to hit TAB or click somewhere else for the event to trigger, hence why onKeyPress
might be better suited. 这意味着用户必须单击TAB或单击其他位置才能触发事件,因此为什么onKeyPress
可能更适合。
Read more: 阅读更多:
http://www.w3schools.com/jsref/event_onchange.asp http://www.w3schools.com/jsref/event_onkeypress.asp http://www.w3schools.com/jsref/event_onchange.asp http://www.w3schools.com/jsref/event_onkeypress.asp
Younger browsers also support onInput
which should certainly be prefered for now, if you do not need to support older browsers. 较年轻的浏览器还支持onInput
,如果您不需要支持较旧的浏览器,那么现在肯定应该首选onInput
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.