2016-11-30 38 views
2

警告我有一個按鈕:防止 '確認導航' 與window.location.href

<button type="button" class="btn btn-primary mt15 send-emails" name="SendEmails" value="true">Send Emails</button> 

與方法:

$('.send-emails').click(function (e) { 
     // build data and other stuff 
     $.post('/leads/sendemails', data, function (data, status, xhr) { 
      alert('Emails have successfully been queued for sending.'); 
      window.onbeforeunload = null; 
      window.location.href = '/Leads/Lead/' + @Model.LeadID; 
    }, 'json'); 
}); 

即使window.onbeforeunload = null;我仍然得到彈出警告:

enter image description here

我如何避免這種情況?

+0

請試試這個:'$(window).unbind();' –

+0

工作正常!列出它作爲答案? –

回答

1

該解決方案如下:

$(window).unbind(); 

這將刪除onbeforeunload事件只是瀏覽器之前重定向到一個新的頁面使用location.href

+0

這不能解決我的問題。 :( 是否有任何其他解決方案此問題?? – M05

+0

這對我起初工作,然後沒有工作後,我解決了這個問題,通過設置'onbeforeundload = function(){}' – Femtosecond

+0

@Femtosecond,你可以添加這是爲了幫助其他人的答案。 –

0

使用全局變量在重定向到像這樣的頁面之前進行檢查。有一個字符串變量來保存href您需要重定向和語句之前重定向它

(function($){ 
    var dontSubmit = false, 
     $form = $('form#my-form'); 

    // Prevent the trigger a false submission on link click 
    window.addEventListener("beforeunload", function (e) { 
     dontSubmit = true; 
     $form.trigger('submit'); 
    }); 

    $form.on('submit', function(event){ 
     if (dontSubmit) { 
      event.preventDefault(); 
      return; 
     } 

     // Continue on as normal 
    }); 
})(jQuery);