Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript window.cofirm使用enter键确认多次重定向_Javascript_Jquery_Browser - Fatal编程技术网

Javascript window.cofirm使用enter键确认多次重定向

Javascript window.cofirm使用enter键确认多次重定向,javascript,jquery,browser,Javascript,Jquery,Browser,以下是显示浏览器确认窗口的“我的代码”: $('a[name="button-paid"]').on('click', function(event) { event.preventDefault(); var href = $(this).attr('href'); if (window.confirm('Are you sure?')) { window.location.href = href; } }); 在我多次尝试按enter键

以下是显示浏览器确认窗口的“我的代码”:

$('a[name="button-paid"]').on('click', function(event) {
     event.preventDefault();
     var href = $(this).attr('href');

    if (window.confirm('Are you sure?')) {
        window.location.href = href;
    }
});
在我多次尝试按enter键确认窗口之前,一切正常

通过多次进入,重定向也会触发多次,并且我的控制器会多次执行其工作

我修复了控制器中的逻辑,以避免多次付款操作,但是否有任何方法可以防止客户端的多次重定向


谢谢。

您可以使用此标志。我不确定,但这可能会有帮助

$('a[name="button-paid"]').on('click', function(event) {
     event.preventDefault();
     var href = $(this).attr('href');
     var flag= true;

    if (window.confirm('Are you sure?')) 
    if (flag){
        flag=false;
        window.location.href = href;

    }
});

第二次按下
a
,当确认窗口关闭时,该按钮将获得焦点

您可以在
事件后添加
$(this).blur()
。preventDefault
以停止此操作

$('a[name="button-paid"]').on('click', function(event) {
    event.preventDefault();
    $(this).blur()

如果你有一张表格,那么你可能也在张贴你的表格

就这样,非常感谢。@DanielLagiň,请投票或将此标记为正确,以供将来可能面临相同问题的其他用户使用。@Sudhakar我知道注释中的答案是不好的做法,但有时值得先通过注释确认问题。在你开始为分数施压之前,给一个人一分钟时间。另外,请阅读:
$('a[name="button-paid"]').on('click', function(event) {
    event.preventDefault();
    $(this).blur()