JQuery.change()只工作一次
我有这段 javascript:
jQuery(document).ready(function(){
const select = $('...'); //it's a simple select
const input = $('...'); //it's a simple input
select.change(doSomething());
input.change(doSomething());
function doSomething(){
console.log("yes");
}
}
yes当页面加载时,在控制台打印两次。
当我更改 select 或 input 的值时yes,控制台不再打印。
jquery version: 3.4.0
回答
您应该将doSomething函数传递给更改参数。不是它的执行结果。
select.change(doSomething);
input.change(doSomething);