2014-03-03 68 views
0

我有幾個階段一種形式,我需要做的jQuery提交表單被點擊的PAG的,左邊的鏈接時:jQuery的鏈接提交表單

<a href='?stage=1' class='page-left-btn'>Stage 1</a> 
<a href='?stage=2' class='page-left-btn'>Stage 2</a> 
<a href='?stage=3' class='page-left-btn'>Stage 3</a> 
<a href='?stage=4' class='page-left-btn'>Stage 4</a> 

<form id='stageform' action='myurl'> 
<!-- form content here which is dynamically dispalyed with php based on the current  stage 
</form> 

現在我有以下的jQuery來提交表單時,單擊鏈接,但它不會工作,我可以爲我的生活工作,爲什麼它不會工作。

$(document).ready(function(){ 
$('a.page-left-btn').click(function() { 
    $('#stageform').submit(); 
}); 

我能得到這個工作,如果我用一個可點擊的div像這樣:

<div class='page-left-btn'>stage 1</div> 

這jQuery的,但我也沒有辦法到新的URL傳遞給網頁?

$(document).ready(function(){ 
$('.page-left-btn').click(function() { 
    $('#stageform').submit(); 
}); 

我是不正確的選擇鏈接,誰能幫助,我敢肯定它的東西簡單,我只是俯瞰

在此先感謝

+0

到'form'提交,您需要在表格上的'action' ...加上,使用'的preventDefault()'爲了避免你的頁面再次加載,一切都會好起來的。 - 查看是否調用方法的一個簡單方法是在'click'事件中添加'console.log('here');'並打開控制檯...您將看到正在調用正確與否。 – balexandre

+0

點擊一個錨標記,提交提交應該點擊右鍵,而不是重定向到'href' – dreamweiver

+0

thankyou提到的網址都爲您的答覆。也許我從來沒有正確解釋過。我在問什麼是如何選擇與某個類的所有鏈接,即這部分是正確的$('a.page-left-btn')。click(function(){ –

回答

2

如果綁定一些JS代碼到一個錨標記,那麼你必須停止錨點的默認行爲,以便將您帶到鏈接的href中指定的下一頁。

所以改成這樣:爲了

$('a.page-left-btn').click(function(e) { 
    e.preventDefault(); // <---this should be done if your selector is anchor 
    $('#stageform').submit(); 
}); 

About event.preventDefault()

+0

啊這是非常有趣的謝謝你。這是我第一次點擊任何一個鏈接,但它只能運行一次,無論我點擊鏈接多少次,頁面不再改變,任何想法 –

+0

當你的表單提交時,將你重定向到一個新頁面,頁面....是否可以使用這個代碼? – Jai

+0

咋它只是在同一頁面上顯示,表單動作是action ='':) –