微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – JQuery阻止链接在ajax加载时工作

我有一个firework爆炸系统,它使用JQuery通过AJAX连接到PHP脚本来引爆烟花.唯一的问题是如果你一个一个地点击一个启动按钮,就有可能引发比你想要的更多的烟火.

我需要一种方法来禁用页面上的所有其他链接,直到ajax完成并收到响应.我试过了:

//Prevent clicks
$("body").find("a").click(function (e) { e.preventDefault(); });
//Re-enable clickable links
$("body").find("a").unbind("click");

我目前的ajax脚本是:

$(document).ready(function() {
$(".button").on("click",function() {
    //disable all other links
    $.ajax({
        type: "POST",
        url: "launch.PHP",
        data: {FID:$(this).attr('id'),Length:$('#FireLength').val()},
        success: function(e) {
            //Re-enable other links once ajax is complete
        }
    });
    return false;
});
});

更好的是,如果按钮在等待响应时变灰.我在http://joshblease.co.uk/firework/一个演示脚本

解决方法:

使用变量禁用的一种方法

$(document).ready(function() {
var disabled = false;
$('a').css('opacity','0.4');
$(".button").on("click",function() {
    //disable all other links
    disabled = true;
    $.ajax({
        type: "POST",
        url: "launch.PHP",
        data: {FID:$(this).attr('id'),Length:$('#FireLength').val()},
        success: function(e) {
            //Re-enable other links once ajax is complete
            disabled = false;
            $('a').css('opacity','1');
        }
    });
    return false;
});
});

$('a').click(function(event){
    if(disabled)
        event.preventDefault();
});

更新

更改了禁用效果链接不透明度.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐