Daily-Question icon indicating copy to clipboard operation
Daily-Question copied to clipboard

【Q214】input 中监听值的变化是在监听什么事件

Open shfshanyue opened this issue 4 years ago • 8 comments

shfshanyue avatar Mar 04 '20 16:03 shfshanyue

可以实时监听值的变化的事件有以下几种

  • keypress
  • keydown
  • keyup
  • input

注: onChange 无法做到实时监听,因为 onChange 需要失去焦点才能触发

shfshanyue avatar Mar 04 '20 16:03 shfshanyue

oninput不用考虑是否失去焦点,不管js操作还是键盘鼠标手动操作,只要HTML元素属性发生改变即可立即捕获到

sunhua-2217 avatar Mar 05 '20 02:03 sunhua-2217

onchange:

Supported HTML tags:

<input type="checkbox">, <input type="file">, <input type="password">, <input type="radio">, <input type="range">, <input type="search">, <input type="text">, <select> and <textarea>

The onchange attribute fires the moment when the value of the element is changed.

Tip: This event is similar to the oninput event. The difference is that the oninput event occurs immediately after the value of an element has changed, while onchange occurs when the element loses focus. The other difference is that the onchange event also works on

qixMan avatar Mar 05 '20 03:03 qixMan

@CaiyueDamowang 测试了好像不行

imondo avatar Mar 06 '20 02:03 imondo

@CaiyueDamowang 测试了好像不行

嗯,不行。。 在html标签中onchange可以在失去焦点触发

cy-98 avatar Mar 07 '20 07:03 cy-98

低版本浏览器 function onPropertyChange(event){console.log(event.srcElement.value)}

ie10以上 <input onInput="onInput(event)" /> function onInput(event){console.log(event.srcElement.value)}

XFwilliamii avatar Sep 14 '20 03:09 XFwilliamii

input 事件

w1687021088 avatar Jul 02 '21 03:07 w1687021088

oninput实时触发,onchange失去焦点时触发

huiaixing avatar Jul 19 '22 06:07 huiaixing