浪跡天涯 程式學習筆記心得

在Event Handler內不透過抓取DOM來獲取Input元素的value

如下:

HTML(Pug)

input(type=“text” oninput=“inputHandler(this.value)”)
// this會指向Input元素

Javascript

function inputHandler(newVal){
console.log(newVal);
// or some other things you want to do with the value
}

※ 適用於onChange, onInput等監聽會被直接裝在要取值的元素上的事件監聽