2013-12-10 58 views
15

我不知道我在做什麼錯在這裏。我希望回車鍵能夠工作以及點擊按鈕。提交表單上的回車鍵使用javascript

<form action="" method="get" class="priceOptionForm" name="priceOptionForm"> 
<input name="paypal_email" type="text" value="whatever" id="email"></label> 
<a href="javascript:void(0);" class="bluebtn" id="profile_price" style="width:60px;margin-top:5px;">Save all</a> 
</form> 
+0

哪一個不工作?輸入鍵或按鈕? – Xymostech

+0

我在這裏找到了解決方案:http://stackoverflow.com/questions/29943/how-to-submit-a-form-when-the-return-key-is-pressed –

回答

19

試試這個:

document.getElementById('email').onkeydown = function(e){ 
    if(e.keyCode == 13){ 
    // submit 
    } 
}; 
+1

爲我工作,T​​Y –

3

使用一個<input type="submit">,而不是鏈接。然後回車鍵將自動工作。

-1

噢,那是因爲HTML表單元素無法識別的鏈接作爲一個按鈕,點擊... 你需要一個按鈕來取代它......

<input type="submit" value="this will display on your button" onClick="javascript:void(0)"> 

,但如果你想讓它看起來像一個鏈接,你應該在css中做到這一點

<style type="text/css"> 
input{background-color:white;border:0 none;} 
</style> 
6

所有下面的代碼應該被添加到腳本塊或文件。 定義提交功能:

function submitForm(){ 
    document.priceOptionForm.submit(); 
    document.priceOptionForm.method='post'; 
} 

對於回車鍵提交表單:

document.onkeydown=function(){ 
    if(window.event.keyCode=='13'){ 
     submitForm(); 
    } 
} 

對於鏈接工作:

document.getElementById("profile_price").onclick=submitForm; 

您可以參考http://jsfiddle.net/honglonglong/YMX2q/一些嘗試。

5

簡單地作出這樣
HTML隱藏按鈕

<input type="submit" id="submitbtn" /> 

CSS

submitbtn{display:none;} 

當用戶將按下回車鍵的形式將提交
不要忘了把type =「submit」

+3

<輸入類型=「submit」style =「display:none;」 /> –

6

請使用下面的代碼片段...它應該被添加到scrip中t區塊

<script> 
    document.onkeydown=function(evt){ 
     var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode; 
     if(keyCode == 13) 
     { 
      //your function call here 
     } 
    } 
</script>