简体   繁体   English

如何允许使用“提交”按钮和Enter键提交表单?

[英]How do I allow form submission with the submit button and the enter key?

I want the user to be able to submit a form by clicking the submit button or the enter key. 我希望用户能够通过单击“提交”按钮或Enter键来提交表单。 I'm using jQuery and here's my code: 我正在使用jQuery,这是我的代码:

    <input id="weather"/>
    <button id="myButton" type="text">Response</button>
</div>

<script type="text/JavaScript">
    $("#myButton").keypress(function(event) {
        if (event.which == 13) {
            event.preventDefault();
            $("#myButton").submit();
        }
    });

    document.getElementById("myButton").onclick = function() {
        if (document.getElementById("weather").value.toLowerCase() != "nice" && document.getElementById("weather").value.toLowerCase() != "crappy") {
            alert("Please enter only either 'Nice' or 'Crappy.'");
        } else if (document.getElementById("weather").value.toLowerCase() == "nice") {
            alert("GREAT! Let's go the the BEACH!!");
        } else {
            alert("Hmm. That blows. Well then, I just won't be going outside today.")
        }
    };
</script>
<input type="text" id="weather" />
<input type="button" id="myButton" value="Response"/>

The onclick script should be onclick脚本应为

document.getElementById("myButton").onclick(function(){
  if (document.getElementById("weather").value.toLowerCase() !="nice" && document.getElementById("weather").value.toLowerCase() !="crappy") {
    alert("Please enter only either 'Nice' or 'Crappy.'");
  } 
  else if (document.getElementById("weather").value.toLowerCase() =="nice"){
    alert("GREAT! Let's go the the BEACH!!");
  } else {
    alert("Hmm. That blows. Well then, I just won't be going outside today.")
  }
});

You should use submit type of button. 您应该使用按钮的提交类型。 It should not be a text type. 它不能是文本类型。

Wrap the whole thing in a form tag and change the button type to submit . 将整个内容包装在一个form标签中,然后将按钮类型更改为submit Enter will then submit the form. 输入,然后提交表格。

<form> 
    <input id="weather"/>
    <button id="myButton" type="submit">Response</button>
</form>

JSFiddle JSFiddle

Best way is to wrap input and button with form and check input only in case of form submit like: 最好的方法是用表单包装输入和按钮,并仅在表单提交时检查输入,例如:

 $(function() { $("#weatherForm").submit(function(event) { event.preventDefault(); var $weatherElement = $('#weather'); if ($weatherElement.val() != "Nice" && $weatherElement.val() != "Crappy") { alert("Please enter only either 'Nice' or 'Crappy.'"); } else if ($weatherElement.val() == "Nice") { alert("GREAT! Let's go the the BEACH!!"); } else { alert("Hmm. That blows. Well then, I just won't be going outside today.") } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="#" id="weatherForm"> <input id="weather" name="weather" /> <input type="submit" id="myButton" value="Response" /> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我有按钮而不是提交时如何使用回车键提交表单 - How to submit form with enter key when I have a button, not a submit 如何在提交按钮 onclick 事件中取消表单提交? - How do I cancel form submission in submit button onclick event? 如何使用回车键禁用表单提交? - How do I disable form submit with enter key? 如果我使用Enter键提交表单,如何防止在Angularjs中提交表单? - How do I prevent form submission in Angularjs if I am submitting the form using enter key? 取消表单提交但允许输入关键事件 - Cancel form submit but allow enter key event 如何启用回车键提交? - How do I enable enter key submit? 如何区分单击提交按钮和输入输入以提交表单? - How do I differentiate between clicking a submit button and hitting enter in an input to submit a form? 如何使用Enter键将项目添加到输入中,并且仅在单击按钮时才提交表单? - How can I add items to an input using the enter key and only submit the form when i click a button? 当不是默认表单操作时,如何使用JavaScript或jQuery通过Enter键提交表单 - How do I use JavaScript or jQuery to submit a form by using the Enter key, when not the default form action 如何发送 <select>使用回车键的表单中没有提交按钮 - How to send a <select> in a form using the enter key without a submit button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM