jquery 多次更改和输入函数 运行
jquery on change and input function run multiple times
在我的代码中有 140 个选择器和 2 个事件(更改、输入)
$("#selector1, #selector2, #selector3, ...... #selector139, #selector140").on('change input',function(e){
console.log('test')//it returns 28 times
//My jquery code
}
当我减少选择器的数量时,return 次数也会减少,但我需要所有的选择器。
我用过这些方法
$("#selector1, #selector2, #selector3, ...... #selector139, #selector140").off('change input').on('change input',function(e){
console.log('test')//it returns 28 times
//My jquery code
}
但输入事件无效
$("#selector1, #selector2, #selector3, ...... #selector139, #selector140").on('change input', function(e){
if(e.handled !== true)
{
//Code
e.handled = true;
}
});
但是这个方法也没用
我使用了 one()
函数,但它没有正常工作。
有什么办法可以解决这个问题吗?
谢谢!!!
最好改用class
。
只需将 class 添加到您的输入中,如下所示:
<input class="someclass" type="text" />
然后您可以像这样轻松添加事件侦听器:
$('.someclass').on('input change',function() {
//do what you want
});
在我的代码中有 140 个选择器和 2 个事件(更改、输入)
$("#selector1, #selector2, #selector3, ...... #selector139, #selector140").on('change input',function(e){
console.log('test')//it returns 28 times
//My jquery code
}
当我减少选择器的数量时,return 次数也会减少,但我需要所有的选择器。
我用过这些方法
$("#selector1, #selector2, #selector3, ...... #selector139, #selector140").off('change input').on('change input',function(e){
console.log('test')//it returns 28 times
//My jquery code
}
但输入事件无效
$("#selector1, #selector2, #selector3, ...... #selector139, #selector140").on('change input', function(e){
if(e.handled !== true)
{
//Code
e.handled = true;
}
});
但是这个方法也没用
我使用了 one()
函数,但它没有正常工作。
有什么办法可以解决这个问题吗?
谢谢!!!
最好改用class
。
只需将 class 添加到您的输入中,如下所示:
<input class="someclass" type="text" />
然后您可以像这样轻松添加事件侦听器:
$('.someclass').on('input change',function() {
//do what you want
});