我有3个文本框,并且对于所有3个的keyup事件,我想调用相同的函数?
在下面的代码中,我想将keyup事件绑定到CalculateTotalOnKeyUpEvent函数到名为compensation的文本框,但它不起作用:
$("#compensation").bind("keyup",CalculateTotalOnKeyUpEvent(keyupEvent)); function CalculateTotalOnKeyUpEvent(keyupEvent) { var keyCode = keyupEvent.keyCode; if (KeyStrokeAllowdToCalculateRefund(keyCode)) { CalculateTotalRefund(); } };
解决方法
你需要这样做:
// 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);