2014-07-22 54 views
0

我有這段代碼,如果一切正常,結果應該是一個感謝頁面。 下面是代碼:無法讓JQuery AJAX的成功事件顯示一個感謝頁面

<a class="large red button round" onclick="$.ajax({ type: 'POST', data:$('#newsletter').serialize(), url: 'http://www.stress-free-life.co.il/lists/?p=subscribe& amp;id=1', success: function (msg) { openWin(); $('#Name').val(''); $('#email').val(''); }, failure: function (msg) { alert('Sorry, we were unable to process your subscription.'); } }); return false;" href="javascript; return false;">לחץ כאן</a> 

我嘗試了好幾種選擇,但不能得到謝謝顯示頁面。

+1

你應該*真正將* onclick移到它自己的函數中。 (可讀性明智,內聯AJAX調用是....屁股疼痛工作) – tymeJV

+0

eewww你必須使用內聯js? – andrew

+0

內聯代碼傷害了我的眼球! –

回答

0

也許這樣的事情...

<a id="ajaxpost" class="large red button round" href="javascript:void(0)">לחץ כאן<a> 


<script> 

$("#ajaxpost").click(function() { 
    $.ajax({ 
     type: 'POST', 
     data: $('#newsletter').serialize(), 
     url: 'http://www.stress-free-life.co.il/lists/?p=subscribe&amp;id=1', 
     success: function (msg) { 
      openWin(); 

      $('#Name').val(''); 
      $('#email').val(''); 
      window.location = "url of thank-you page"; 
     }, 
     failure: function (msg) { 
      alert('Sorry, we were unable to process your subscription.'); 
     } 
    }); 
}); 

</script> 
+0

它仍然沒有打開網址的謝謝頁 –

1

嗯......讓我們打掃一下,onclick處理是真的難以閱讀,通常被認爲是不好的做法

你的HTML:

<a class="large red button round my-button" href='#'>לחץ כאן</a> 

而在一個單獨的JS文件中,您應該有:

$(function(){ 
    $('.my-button').on('click', function(e){ 
    e.preventDefault(); 

    $.ajax({ 
     type: 'POST', 
     data: $('#newsletter').serialize(), 
     url: 'http://www.stress-free-life.co.il/lists/?p=subscribe&id=1', 
     success: function(msg){ 
     window.location = "/my-thank-you-page.html"; 
     //openWin(); // No idea what this function is... 
     //$('#Name').val(''); 
     //$('#email').val(''); 
     }, 
     failure: function(msg){ 
     alert('Sorry, we were unable to process your subscription.'); 
     } 
    }); 
    }); 
}); 
+0

你的好多了:) –

+0

它仍然沒有打開謝謝頁 –

+0

在你的JS控制檯中是否有javascript錯誤?你看到什麼行爲?你有沒有把你的感謝網址放在window.location區域? – nzifnab