propertychange / inputchange 事件需失去焦點才觸發,無法即時響應。跨瀏覽器方案:
// IE 專屬 propertychange,現代瀏覽器用 input 事件
$('#flowfromid').on('input propertychange', function() {
// 此處處理即時邏輯,如字數統計、即時搜尋
console.log('值已變更:', this.value);
});
$(document).ready(function() {
$('#txt_JumpPager').on('keydown', function(e) {
if (e.which === 13) { // 13 = Enter
$('#lbtn_JumpPager').trigger('click');
return false; // 防止表單預設提交
}
});
});
$('#txtEmail').trigger('focus');
$(function() {
$(':input').on('focus', function() {
$(this).addClass('focus');
}).on('blur', function() {
$(this).removeClass('focus');
});
});
配合 CSS:
.focus { border-color: #4a90e2; box-shadow: 0 0 4px rgba(74,144,226,0.4); }
| 需求 | 推薦事件 | 備註 |
|---|---|---|
| 即時偵測輸入 | input (現代) / propertychange (IE≤11) | 合併寫 on('input propertychange') |
| 失去焦點後驗證 | change / blur | change 僅值改變時觸發 |
| Enter 鍵提交 | keydown 判斷 e.which===13 | 勿用 keypress(已棄用) |
| 獲得焦點 | focus / focusin (冒泡) | focusin 可委派 |
| 失去焦點 | blur / focusout (冒泡) | focusout 可委派 |
propertychange 僅 IE 支援,現代專案請優先用 input,並配合 $.support 或直接雙綁。keydown 中 return false 等同 e.preventDefault() + e.stopPropagation()。$(document).on('input', '.dynamic-input', fn)。