为什么 'input' 事件不会在文件输入时触发,但 'change' 事件会触发?
Why do 'input' events not fire on file inputs, but 'change' events do?
测试简单输入:
<input type="file"/>
选择新文件时 input
事件不会触发:
$('input').on('input', function(event){
console.log('input value changed', event.target.value)
})
change
事件触发:
$('input').on('change', function(event){
console.log('input value changed', event.target.value)
})
为什么 'input' 事件没有触发?
输入事件支持的元素仅限于textArea,
输入类型=文本,或输入类型=密码。来自 MSDN:
Input event occurs when the text content of an element is changed
through the user interface.
You can use the oninput to detect when the contents of a textArea,
input type=text, or input type=password have changed. This event
occurs immediately after modification, unlike the onchange event,
which occurs when the element loses focus.
根据W3.org,对于文件输入,"The input event does not apply."(见簿记细节下的最后一个项目符号)。
测试简单输入:
<input type="file"/>
选择新文件时 input
事件不会触发:
$('input').on('input', function(event){
console.log('input value changed', event.target.value)
})
change
事件触发:
$('input').on('change', function(event){
console.log('input value changed', event.target.value)
})
为什么 'input' 事件没有触发?
输入事件支持的元素仅限于textArea, 输入类型=文本,或输入类型=密码。来自 MSDN:
Input event occurs when the text content of an element is changed through the user interface.
You can use the oninput to detect when the contents of a textArea, input type=text, or input type=password have changed. This event occurs immediately after modification, unlike the onchange event, which occurs when the element loses focus.
根据W3.org,对于文件输入,"The input event does not apply."(见簿记细节下的最后一个项目符号)。