bind same event on two variable

时间:2021-04-29 23:57:40
var office = $('#gameid') || $('#netid'); 

//$("#gameid" && "#netid").on('change', function(){


office.on('change', function(){

I want that if there is a change between one of the var office, then the function will be there is one event.

我希望如果其中一个var办公室之间有变化,那么该函数将有一个事件。

How to do that?

怎么做?

3 个解决方案

#1


6  

Try using .add() function to club the both,

尝试使用.add()函数来支持两者,

var office = $('#gameid').add($('#netid')) 
office.on('change', function(){

Apart from the solution, i just need to comment on what you tried,

除了解决方案,我只需要评论你的尝试,

var office = $('#gameid') || $('#netid'); 

The above code of yours always return $('#gameid') to the variable office, because Jquery object is basically a collection. And it won't be undefined/falsy at any point of time.

你的上述代码总是将$('#gameid')返回给变量office,因为Jquery对象基本上是一个集合。并且在任何时候它都不会是不确定/错误的。

#2


5  

Use , to pass multiple selectors

使用,传递多个选择器

$("#gameid, #netid").on('change', function(){

#3


0  

Try with -

尝试 -

$('#gameid, #netid').on('change', function() { ... })

#1


6  

Try using .add() function to club the both,

尝试使用.add()函数来支持两者,

var office = $('#gameid').add($('#netid')) 
office.on('change', function(){

Apart from the solution, i just need to comment on what you tried,

除了解决方案,我只需要评论你的尝试,

var office = $('#gameid') || $('#netid'); 

The above code of yours always return $('#gameid') to the variable office, because Jquery object is basically a collection. And it won't be undefined/falsy at any point of time.

你的上述代码总是将$('#gameid')返回给变量office,因为Jquery对象基本上是一个集合。并且在任何时候它都不会是不确定/错误的。

#2


5  

Use , to pass multiple selectors

使用,传递多个选择器

$("#gameid, #netid").on('change', function(){

#3


0  

Try with -

尝试 -

$('#gameid, #netid').on('change', function() { ... })