2017-07-25 73 views
0

我有一个简单的JavaScript代码,如下所示,我想在淡出和淡入文件后重新加载页面,但我不知道应该在哪里放window.location.reload();函数。你有什么建议吗 ?我是JavaScript新手。功能执行后重新加载页面

<script>$(document).ready(function() { 

     $('.updateButton').on('click', function() { 
     var member_id = $(this).attr('member_id'); 
     var city = $('#city'+member_id).val(); 
     var continent = $('#continent'+member_id).val(); 
     var country = $('#country'+member_id).val(); 
     var id = $('#id'+member_id).val(); 

     req = $.ajax({ 
      url : '/deleteRequest', 
      type : 'POST', 
      data : 
       {city : city,continent : continent,country : country,id : id} 
     }); 
     req.done(function(data) { 
      $('#city'+member_id).fadeOut(500).fadeIn(500); 
      $('#continent'+member_id).fadeOut(500).fadeIn(500); 
      $('#country'+member_id).fadeOut(500).fadeIn(500); 
      $('#id'+member_id).fadeOut(500).fadeIn(500); 
      }); 

    }); 
}); 
</script> 
+2

在完成功能通常 – madalinivascu

+0

req done函数的结尾,也许是setTimeout(“window.location.reload()”,2000);更好。 – Nico

回答

1

下面是更新后的代码,你可以去看看,并尝试内。我已合并您的所有元素,并且fadeIn有一个回调,您可以在其中刷新页面。因此,当fadeIn完成后页面会重新加载只:

$('#city' + member_id + ',#continent' + member_id + ', #country' + member_id + ', #id' + member_id).fadeOut(500).fadeIn(500, function() { 
    location.reload(); 
}); 
1

把它放在一个超时功能

req.done(function(data) { 
        $('#city'+member_id).fadeOut(500).fadeIn(500); 
        $('#continent'+member_id).fadeOut(500).fadeIn(500); 
        $('#country'+member_id).fadeOut(500).fadeIn(500); 
        $('#id'+member_id).fadeOut(500).fadeIn(500); 
        setTimeout(function(){ 
        window.location.reload(); 
         },TotalTimeNeededbyThheAnimationInmiliseconds) 
        }); 

      }); 
+1

'windows.location.href()'不是一个函数。你的意思是'window.location.reload()'? –

+0

我没有正确地认为它只是一个观点,你使用你的代码那里男人 –

1
req.done(function(data) { 
     $('#city'+member_id).fadeOut(500).fadeIn(500); 
     $('#continent'+member_id).fadeOut(500).fadeIn(500); 
     $('#country'+member_id).fadeOut(500).fadeIn(500); 
     $('#id'+member_id).fadeOut(500).fadeIn(500); 
     window.location = ''; // This will reload your page. 
}); 
+0

@Szymoñ,这工作? –

+0

它不起作用 –

1

你应该尝试

req.always(function(){ 
    window.location.reload(); 
}); 

它会永远工作,你是否得到resonse或not.if要重新加载,当你得到你的反应可以查看教程here

1
在最新的jQuery

取消通知:

The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks will be deprecated in jQuery 1.8. To prepare your code for their 

最终消除,使用jqXHR.done(),jqXHR.fail(),和jqXHR.always() 代替。

使用.done().fail().always()代替success()error()complete()

我相信你想淡入淡出,只隐藏AJAX成功,所以你把它放在done()

,如果你想每次重装阿贾克斯结束使用always()

如:

req.always(function(){ 
    window.location.reload(false); 
// If we needed to pull the document from 
// the web-server again (such as where the document contents 
// change dynamically) we would pass the argument as 'true'. 
}); 

,如果你希望它无论是在done() 然后

req.done(function(){ 
// you fadein,fadeout code 

window.location.reload(); 
});