[英]How to send a <select> in a form using the enter key without a submit button
如標題所述,有可能嗎? 還是我真的需要一個提交按鈕來提交它?
我嘗試了一些方法,但不幸的是,它對我不起作用,或者代碼中有錯誤。
這是我嘗試過的代碼:
document.getElementById("select_id").addEventListener('keypress', function(event) {
if(event.keyCode == 13) {
document.getElementById("form_id").submit();
};
});
這是html:
<form id="form_id" action="processor.php" method="POST">
<select id="select_id" name="chosenProduct">
<option value="product_a">Product A</option>
<option value="product_b">Product B</option>
</select>
</form>
你好試試這個
添加event.preventDefault();
在document.getElementById("form_id").submit();
我想你錯過了這一點。
如果那時仍然不起作用
使用按鍵功能使用以下代碼
/* without button*/
$(document).ready(function(){
$("#select_id").keypress(function(event) {
if (event.which == 13) {
event.preventDefault();
$("#form_id").submit();
}
});
});
/ *使用按鈕按鍵事件* /
$("#select_id").keyup(function(event){
if(event.keyCode == 13){
$("#id_of_button").click();
}
});
希望這對您有所幫助。
使用onkeypress在Enter上提交表單
<form id="form_id" action="" method="POST">
<select id="select_id" name="chosenProduct" onkeypress="document.getElementById('form_id').submit()">
<option value="product_a">Product A</option>
<option value="product_b">Product B</option>
</select>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.