【问题标题】:Mobile Browser fires click/tap event twice移动浏览器触发两次点击/点击事件
【发布时间】:2014-10-24 23:00:15
【问题描述】:

我有一个移动网络应用程序,当点击一个按钮时,它会自动为你创建一个玩家团队。第一次你没有收到任何警告,因为你没有球员,但是如果你的团队中已经有人,你会收到警告说他们将被删除。

此错误仅在您快速点击按钮时发生,但如果您按住按钮任何时间,它只会触发一次。

有没有办法让点击事件只触发一次?这在 iOS 和 Android (4.3) 上都会发生?

代码如下:

$(document).on('click tap','.autoTeam',function(e){
    $.ajax({
        type:"POST",
        url:alink+"assets/scripts/myteam-ajax-0.2",
        data:"page=autoteamCheck&tid="+tid,
        success:function(data2)
        {
            if(data2['totalPlayers'] == 0)
            {
                var c = true;
            }
            else
            {
                c=confirm('Using the auto create will remove all players from your team, are you sure you wish to continue?');
            }
            if(c == true)
            {
                $.ajax({
                    type:"POST",
                    url:alink+"assets/scripts/myteam-ajax-0.2",
                    data:"page=autoteam&tid="+tid+"&lid="+lid,
                    success:function(data)
                    {
                        if(data['error'] == 'Y')
                        {
                            alert(data['message']);
                        }
                        else
                        {
                            window.location.reload();
                        }
                    },
                    dataType:"json"
                });
            }
        },
        dataType:"json"
    });
    e.stopImmediatePropagation();
    e.gesture.preventDefault();
    e.preventDefault(); return false;
});

<input class="button autoTeam" data-role="none" value="Auto Create" type="button"  style="margin-right:20px; font-size:0.8em;"/>

【问题讨论】:

  • 改用vclick
  • .autoTeam 是动态添加的吗?如果没有,那么$('.autoTeam').on('click', function () { }); 应该可以工作。
  • 不是它是在屏幕上创建的,点击是否适用于所有设备?我认为这就是使用点击和点按的原因?
  • 你应该使用我假设的一个事件,点击、点击、vclick。 vclick 适用于所有。
  • iOS 上的 Safari 不接受点击事件。

标签: android jquery html ios jquery-mobile


【解决方案1】:

我遇到了同样的问题。我的解决方案是在事件处理程序的末尾添加return false;。示例:

$(document).on("click, tap", ".autoTeam", function(event){
    // do thing
    return false; // double event fix
}); 

【讨论】:

  • 一个老问题。添加 event.stopImmediatePropagation 最终成为我使用的解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多