简体   繁体   English

如何使输入单击按钮

[英]How to make enter click a button

I have a button in my form that simply unhides a div, which contains the submit button for the form. 我的表单中有一个按钮,可以简单地取消隐藏div,其中包含该表单的提交按钮。 I am using the script 我正在使用脚本

I want the user when pressing enter (whilst inside the text input field) to simply click the button and not submit the form. 我希望用户在按Enter键时(在文本输入字段内)仅单击按钮而不提交表单。 Currently when pressing the enter key I can see the button being clicked for a split second before the form is submitted. 目前,当按下回车键时,我可以看到在提交表单之前,瞬间就单击了该按钮。 How do I make it so a certain input area can click enter and click the button, but another text area pressing enter submits the final form. 如何使某个输入区域可以单击Enter并单击按钮,但是另一个按Enter的文本区域则提交最终表单。

I know I can use this script however it stops the enter key from working on the submit form input. 我知道我可以使用此脚本,但是它会阻止Enter键在提交表单输入中起作用。

window.addEventListener('keypress', function (e) {
if (e.keyCode === 13) {
    e.preventDefault();
    feedTheList();
}
});

This is the input that I want the user to be able to press enter on to click the button shown below. 这是我希望用户能够按下Enter键单击以下所示按钮的输入。

<label for="userAccountName">username</label><br>
        <input class="textField" type="text" name="username" 
id="userAccountName" maxlength="64" tabindex="1" value=""><br>&nbsp;<br>
        <label for="userPassword">Password</label><br>
        <input class="textField" type="password" name="password"
id="userPassword" autocomplete="off" maxlength="64" tabindex="2"><br>

This is the button input that shows the hidden div (I want the enter key when inside the above input to click this button below. 这是显示隐藏的div的按钮输入(我希望在上面的输入内有Enter键,请在下面单击此按钮。

<input class="btn_green_white_innerfade btn_medium" type="button" 
name="submit" id="SteamLogin" value="Sign in" width="104" height="25" 
border="0" tabindex="5" onclick="showDiv()">

This is the hidden div input, I want the enter key on this input to submit the form 这是隐藏的div输入,我希望此输入上的enter键提交表单

<input name="authcode" class="twofactorauthcode_entry_input 
authcode_placeholder" id="twofactorcode_entry" type="text" 
placeholder="enter your code here" autocomplete="off"/>

All of these inputs are inside of the following form 所有这些输入都在以下表格中

<form action="/loginaction.php" method="post" name="submit" id='submit'>

and finally the showDiv() function is 最后,showDiv()函数是

function showDiv() {
document.getElementById('SteamLogin').style.display = "none";
document.getElementById('loadingGif').style.display = "block";
setTimeout(function() {
document.getElementById('loadingGif').style.display = "none";
var username = document.getElementById("userAccountName").value;
document.getElementById("login_twofactorauth_message_entercode_accountname")
.innerText = username
document.getElementById('showme').style.display = "block";
document.getElementById('unhideme').style.display = "block";
},2000);

You could add event listeners to the individual inputs - The #twofactorcode_entry input will submit the form when enter is pressed, while the .textField inputs will click the button. 您可以将事件侦听器添加到各个输入中- #twofactorcode_entry输入将在按下Enter键时提交表单,而.textField输入将单击按钮。

 const inputs = document.querySelectorAll('.textField'); const button = document.getElementById('SteamLogin'); const form = document.getElementById('form'); const twofactor = document.getElementById('twofactorcode_entry'); for (var i = 0; i < inputs.length; i++) { inputs[i].addEventListener('keypress', e => { if (e.key === 'Enter') { e.preventDefault(); button.click(); } }); } button.addEventListener('click', e => { // submit the form instead of showing the hidden input if the input has text // if (twofactor.value.length) form.submit(); showDiv(); }); twofactor.addEventListener('keypress', e => { if (e.key === 'Enter') { e.preventDefault(); form.submit(); alert('submitted form'); } }); function showDiv() { twofactor.classList.remove('hidden'); } 
 .hidden { display:none; } 
 <form id="form"> <label for="userAccountName">username</label><br> <input class="textField" type="text" name="username" id="userAccountName" maxlength="64" tabindex="1" value=""><br>&nbsp;<br> <label for="userPassword">Password</label><br> <input class="textField" type="password" name="password" id="userPassword" autocomplete="off" maxlength="64" tabindex="2"><br> <!-- removed name="submit" so form.submit() works --> <input class="btn_green_white_innerfade btn_medium" type="button" id="SteamLogin" value="Sign in" width="104" height="25" border="0" tabindex="5"><br> <input name="authcode" class="twofactorauthcode_entry_input authcode_placeholder hidden" id="twofactorcode_entry" type="text" placeholder="enter your code here" autocomplete="off"/> </form> 

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

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