I have 3 textboxes, and on the keyup
event for all 3, I want to call the same function?
我有3个文本框,并且对于所有3个的keyup事件,我想调用相同的函数?
In the below code, I am tring to bind keyup
event to CalculateTotalOnKeyUpEvent
function to a textbox named compensation
, but it doesn't work:
在下面的代码中,我想将keyup事件绑定到CalculateTotalOnKeyUpEvent函数到名为compensation的文本框,但它不起作用:
$("#compensation").bind("keyup", CalculateTotalOnKeyUpEvent(keyupEvent));
function CalculateTotalOnKeyUpEvent(keyupEvent) {
var keyCode = keyupEvent.keyCode;
if (KeyStrokeAllowdToCalculateRefund(keyCode)) {
CalculateTotalRefund();
}
};
5 个解决方案
#1
You need do like this:
你需要这样做:
// Edit according to request in the comment:
// in order to select more than one element,
// you need to specify comma separated ids.
// Also, maybe you need to consider to use a CSS class for the selected elements,
// then it could be just $(".className")
$("#element1, #element2, ....").bind("keyup", CalculateTotalOnKeyUpEvent);
You need to pass the function as a parameter, you do not need to pass the function as it was declared.
您需要将函数作为参数传递,您不需要传递声明的函数。
#2
$("#txt1, #txt2, #txt3").keyup(fn);
#3
or just give all 3 text boxes the same class and you good to go
或者只是给所有3个文本框都是同一个类,你就可以了
#4
$("#compensation").bind("keyup",CalculateTotalOnKeyUpEvent(keyupEvent));
When you write CalculateTotalOnKeyUpEvent(keyUpEvent) [notice the () after function name], you are executing the CalculateTotalOnKeyUpEvent and assigning the result of the function to the key up event.
当您编写CalculateTotalOnKeyUpEvent(keyUpEvent)[注意函数名称之后的()]时,您正在执行CalculateTotalOnKeyUpEvent并将函数的结果分配给key up事件。
You should do something like,
你应该做的事情,
$('#compensation, #otherId1, #otherId2')
.bind('keyup', CalculateTotalOnKeyUpEvent);
Where 'otherId1' and 'otherId2' are the ids of two more textboxes.
其中'otherId1'和'otherId2'是另外两个文本框的ID。
#5
You are calling CalculateTotalOnKeyUpEvent
immediately, not passing it as an argument. Try this:
您正在立即调用CalculateTotalOnKeyUpEvent,而不是将其作为参数传递。试试这个:
$("#compensation").bind("keyup",CalculateTotalOnKeyUpEvent);
#1
You need do like this:
你需要这样做:
// Edit according to request in the comment:
// in order to select more than one element,
// you need to specify comma separated ids.
// Also, maybe you need to consider to use a CSS class for the selected elements,
// then it could be just $(".className")
$("#element1, #element2, ....").bind("keyup", CalculateTotalOnKeyUpEvent);
You need to pass the function as a parameter, you do not need to pass the function as it was declared.
您需要将函数作为参数传递,您不需要传递声明的函数。
#2
$("#txt1, #txt2, #txt3").keyup(fn);
#3
or just give all 3 text boxes the same class and you good to go
或者只是给所有3个文本框都是同一个类,你就可以了
#4
$("#compensation").bind("keyup",CalculateTotalOnKeyUpEvent(keyupEvent));
When you write CalculateTotalOnKeyUpEvent(keyUpEvent) [notice the () after function name], you are executing the CalculateTotalOnKeyUpEvent and assigning the result of the function to the key up event.
当您编写CalculateTotalOnKeyUpEvent(keyUpEvent)[注意函数名称之后的()]时,您正在执行CalculateTotalOnKeyUpEvent并将函数的结果分配给key up事件。
You should do something like,
你应该做的事情,
$('#compensation, #otherId1, #otherId2')
.bind('keyup', CalculateTotalOnKeyUpEvent);
Where 'otherId1' and 'otherId2' are the ids of two more textboxes.
其中'otherId1'和'otherId2'是另外两个文本框的ID。
#5
You are calling CalculateTotalOnKeyUpEvent
immediately, not passing it as an argument. Try this:
您正在立即调用CalculateTotalOnKeyUpEvent,而不是将其作为参数传递。试试这个:
$("#compensation").bind("keyup",CalculateTotalOnKeyUpEvent);