oninput没有在复选框上启动吗?

时间:2021-03-03 19:37:51

The following code does not work on all input elements:

以下代码不适用于所有输入元素:

$(':input').on('input', function(){
  alert('boom shakalaka');
});

It has no effect on check-boxes even though they are input elements, and checking or unchecking changes its state.

它对复选框没有影响,即使它们是输入元素,检查或取消检查会改变它的状态。

I can add the change event too inside on() but then the same event will be fired twice on other input fields.

我也可以在()中添加change事件,但是相同的事件将在其他输入字段中被触发两次。

Is there any reliable input state-change event that is consistent for all form elements?

是否存在所有表单元素都一致的可靠输入状态更改事件?

2 个解决方案

#1


5  

There is no single event that would be authoritative, but you can bind to multiple events -

没有一个事件是权威的,但是您可以绑定到多个事件—

$(':input').on('change keyup input', function() {
    console.log('changed');
});

P.S. I wanted to give you a +1 for using 'boom shakalaka' in an example, but I digress.

我想给你们一个+1的例子,用boom shakalaka,但是我离题了。

#2


-1  

I'm not sure if what you want but maybe a simple blur event?

我不知道你想要什么,也许是一个简单的模糊事件?

$('input').on('blur',function(e){
      alert('event');
    });

#1


5  

There is no single event that would be authoritative, but you can bind to multiple events -

没有一个事件是权威的,但是您可以绑定到多个事件—

$(':input').on('change keyup input', function() {
    console.log('changed');
});

P.S. I wanted to give you a +1 for using 'boom shakalaka' in an example, but I digress.

我想给你们一个+1的例子,用boom shakalaka,但是我离题了。

#2


-1  

I'm not sure if what you want but maybe a simple blur event?

我不知道你想要什么,也许是一个简单的模糊事件?

$('input').on('blur',function(e){
      alert('event');
    });