Javascript会触发两个事件 - onkeypress和onclick [英] Javascript fires two events - onkeypress and onclick

查看:126
本文介绍了Javascript会触发两个事件 - onkeypress和onclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是当我按一个单选按钮,元素MyFunc会触发两次 - 一次为onkeypress事件,另一次为点击事件。



问题为什么?我需要通过两种不同的方式来处理这个问题,但是现在我不知道什么是最初的事件。当我点击鼠标时,它将触发点击事件。

 < ul> 
< li>
< input type =radioonkeypress =MyFunc(event,this)onclick =MyFunc(event,this)name =myListid =MyId_1/>主题1
< input type =radioonkeypress =MyFunc(event,this)onclick =MyFunc(event,this)name =myListid =MyId_2/>主题2
& /立GT;
< / ul>

函数MyFunc(e,obj){
alert(e.type); // alertkeypress然后点击
//我的东西
}

谢谢你的帮助!

解决方案

onclick 当单选按钮被选中时被触发。由于您通过按键选择它,两个事件将被触发。首先是 onkeypress 事件,然后是 onclick 事件。


Problem is when I press a key over a radio button, element MyFunc fires twice - once for "onkeypress" event, another time for "click" event.

Question "Why?" I need to handle this by two different ways, but now I can not recognize what initial event was. When I click a mouse it fires just for "click" event.

<ul>
    <li>
        <input type="radio" onkeypress="MyFunc(event, this)" onclick="MyFunc(event, this)" name="myList" id="MyId_1" />Topic 1
        <input type="radio" onkeypress="MyFunc(event, this)" onclick="MyFunc(event, this)" name="myList" id="MyId_2" />Topic 2
    </li>
</ul>

function MyFunc(e, obj) {
    alert(e.type); // alerts "keypress" and then "click"
    // Do my stuff
}

Thank you for help!

解决方案

The onclick Event is fired, when the radio button gets selected. Since you select it by pressing a key, both events will get fired. First the onkeypress event and then the onclick event.

这篇关于Javascript会触发两个事件 - onkeypress和onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆