简体   繁体   English

仅当所有字段均已填写时,如何在按“ Enter”键时接受值?

[英]How to accept values on pressing “Enter” only if all fields are full?

So I have this code in place that accepts 2 fields "userNAme" and "yourMessage". 因此,我在此代码中接受了两个字段“ userNAme”和“ yourMessage”。 The "Send" button only activates if both the fields are filled up. 仅当两个字段都填写时,“发送”按钮才激活。

 $("input[type='text'],textarea").keyup(function() { var text = ($(this).val()).toString(); text = text.replace(/\\s+/g, " ").trim(); var message = ($("textarea").val()).toString(); message = message.replace(/\\s+/g, " ").trim(); if (text != "" && message != "") { $('input[type="button"]').removeAttr('disabled'); } else { $("#send").attr('disabled', 'disabled'); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h1>Hello!</h1> <form action="" method="post" id="subscribe" name="subscribe"> First name: <input type="text" name="fname" id="fname" placeholder="Name" autofocus><br> <div id="message"></div> Your Message:<br> <textarea typeof="text" id="yourMessage" placeholder="Start Typing..."></textarea><br /> <input type="button" class="button button-blue" value="Send" name="button" disabled="disabled" id="send" /> </form> <input type="button" class="button button-blue" value="Click Me!" id="click-me" /> <div id="message"></div> 

But now I want to add the functionality of Entering the values on key press of "Enter". 但是现在我想添加在按“ Enter”键时输入值的功能。 I want this to work like the "Send" button, ie only work if all fields are filled up. 我希望它像“发送”按钮一样工作,即仅在所有字段均已填写的情况下工作。 How to go about this? 怎么办呢?

If you add the required attribute into your input tag when you click on Submit or hit the Enter key, all fields must be already completed in order to be submited. 如果在单击“提交”或按Enter键时将required属性添加到input标记中,则所有字段都必须已经完成才能提交。

For example: 例如:

<input type="text" name="fname" id="fname" placeholder="Name" required autofocus>

if you were to use a button with type submit the form will not be submitted by pressing enter as long as that button is disabled, like below 如果你使用一个button类型submit表单不会按只要该按钮被禁用,就像在下面输入提交

 $("input[type='text'],textarea").keyup(function() { var text = ($(this).val()).toString(); text = text.replace(/\\s+/g, " ").trim(); var message = ($("textarea").val()).toString(); message = message.replace(/\\s+/g, " ").trim(); if (text != "" && message != "") { $('button[type="submit"]').removeAttr('disabled'); } else { $("#send").attr('disabled', 'disabled'); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h1>Hello!</h1> <form action="" method="post" id="subscribe" name="subscribe"> First name: <input type="text" name="fname" id="fname" placeholder="Name" autofocus><br> <div id="message"></div> Your Message:<br> <textarea typeof="text" id="yourMessage" placeholder="Start Typing..."></textarea><br /> <button type="submit" class="button button-blue" name="button" disabled="disabled" id="send">Send</button> </form> <input type="button" class="button button-blue" value="Click Me!" id="click-me" /> <div id="message"></div> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM