[英]How to stack a key event and a click event from two different elements to trigger the same function
Is there a way to combine both an click
event from an img
element and a onkeypress
event from an input element to trigger the same function?. 有没有一种方法可以将
img
元素中的click
事件和输入元素中的onkeypress
事件组合在一起,以触发相同的功能? This is what I got so far and it only services the click event... 这就是我到目前为止所得到的,它仅适用于click事件。
<div class="dataLine2"> <img id ="daysText" src="images/days.png"> <img class ="days" src="images/1day.png"> <img class ="days" src="images/2days.png"> <img class ="days" src="images/3days.png"> <img class ="days" src="images/4days.png"> </div>
<input type="text" class="stateInput" placeholder="State" maxlength="2" value="NY" onkeypress="myFunction(???)" />
<script>
// I suspect that jQuery can stack two triggers in some daisy chain, but it eludes me...
$('.days').on('click', function (e){
// Do somehting crazy with the "e" being critical to the process in this function
//
}
</script>
Any help would be appreciated... 任何帮助,将不胜感激...
Dennis 丹尼斯
Just write your handler function and assign it: 只需编写处理程序函数并分配它:
function foo(e) {
// do something
}
$('.days').on('click', foo);
$('input').on('keypress', foo);
I was ducked 2 points for posting this question which purportedly is not original. 我因张贴此问题(据称不是原创问题)而被忽略了2分。 Unfortunately, the other question/solution does not concern the same idea.
不幸的是,另一个问题/解决方案并不涉及相同的想法。
The unanimous solution for the other problem was: 另一个问题的一致解决方案是:
`$('#element').on('keyup keypress blur change', function(event) {`
That is useless to me. 对我来说那没用。 In my setting I am not firing from the same element.
在我的设置中,我不是从同一元素中解雇。 I have 5 different elements each of which sends a trigger to the same function.
我有5个不同的元素,每个元素都向同一函数发送一个触发器。 And that is clearly stated in my post AND in the title!!!
这在我的帖子和标题中已明确说明!!!
So I should receive my 2 points back and then allow other points to pile on because I asked an original question, which has not yet been answered. 所以我应该拿回我的2分,然后再增加其他分,因为我问了一个原始问题,但尚未回答。
DK DK
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.