douhua9726 2013-09-24 22:54
浏览 33
已采纳

event.preventDefault(); 停止在FF工作

I have the following code, which did work fine until just now, event.preventDefault(); doesn't seem to trigger in FireFox? In Chrome the code still works fine, in Firefox however, it takes me to blank page and I see generated code there instead.

    $("#generate_code").click(function(){
        event.preventDefault();
        $('#code').html('Generating Code..');
        hideshow('loading',1);
        setTimeout(function() {
            $.get("generate-code.php", function(data) {
                $("#code").html(data);
            });
            hideshow('loading',0);
        }, 2000);
   });

generate-code.php:

function generateRandomString($length = 50) {
    $characters = '!@#$%^&*()0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
}

$invitecode = generateRandomString();
echo $invitecode;
  • 写回答

1条回答 默认 最新

  • dsu89430 2013-09-24 22:57
    关注

    You should be calling preventDefault on the event object passed as argument to the handler to make sure that everything works cross-browser.

    $("#generate_code").click(function(e){
        e.preventDefault();
        ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗