jQuery~覆盖了两个点击事件?

时间:2010-09-17 13:59:12

标签: javascript jquery events

我有一个容器元素说

<div id="myContainer">
  <form action="" method="post">
   Radio: <input type="radio" name="myRadio" id="myRadio" value="1" />
  </form>
</div>

因此,如果我将点击事件分配给$('#myContainer'),并且点击事件到$('#myRadio')有没有办法让他们同时开火?目前,因为表单元素“上方”有一个点击事件,它有一个return false来阻止页面跳转,我无法点击单选按钮。

有没有办法解决这个问题?或者我需要在我的选择器中更具体?甚至是$('#myContainer:not("#myRadio")')? (这甚至有效吗?)

2 个答案:

答案 0 :(得分:0)

要停止页面跳转,请使用event.preventDefault()代替return false;,而return false目前正在停止事件的发展。

自然行为适用于the event to bubble,所以你需要做的就是让两个处理程序都不会通过{{1}}干扰气泡:)

答案 1 :(得分:0)

您可以执行以下操作!

$('#myContainer, #myRadio').click(function(event){

    event.stopPropagation(); //This will stop the bubble so it only fires once per element
    switch(event.srcElement)
    {
        case 'HTMLFormElement':
            //The Div / Form
        break;
        case 'HTMLInputElement':
            //The input itself
        break;   
    }
    return false;
})​

示例Here,您需要添加提醒

相关问题