gpt4 book ai didi

JavaScript 带条件重置输入

转载 作者:行者123 更新时间:2023-12-03 03:03:27 25 4
gpt4 key购买 nike

如果 loginDiv 显示为空,我正在尝试重置登录的一些输入字段,但它根本不起作用。

期望结果:点击登录按钮,只要loginDiv显示为空,两个输入字段就会被重置。

这是我的代码:

// LOGIN
const logDiv = document.getElementById('loginDiv');
const wrong = document.getElementById('wrong');
const user = document.getElementById('user');
const pass = document.getElementById('pass');
const log = document.getElementById('logBtn');

log.addEventListener('click', () => {
if (user.value == 'f' && pass.value == 'f') {
logDiv.style.display = 'none';
wrong.style.display = 'none';
} else {
wrong.style.display = 'block';
}
});

// LOGOUT

const logout = document.getElementById('logout');

logout.addEventListener('click', () => {
if (logDiv.style.display == 'none') {
logDiv.style.display = 'block';
}
});

function resetForm() {
if (logDiv.style.display == 'none') {
user.reset();
pass.reset();
}
}
<div id="loginDiv">
<input id="user" type="text" placeholder="username"></input>
<input id="pass" type="password" placeholder="password"></input>
<button id="logBtn" onclick="resetForm()">login</button>
</div>
<div id="wrong" style="display: none;">
<p id="wrongText">incorrect login info.</p>
</div>

<div id="logoutDiv">
<button id="logout" type="button">logout</button>
</div>

最佳答案

用户名和密码验证通过后,您可以将用户名和密码文本框的值设置为空字符串。

user.value = "";
pass.value = "";

// LOGIN
const logDiv = document.getElementById('loginDiv');
const wrong = document.getElementById('wrong');
const user = document.getElementById('user');
const pass = document.getElementById('pass');
const log = document.getElementById('logBtn');

log.addEventListener('click', () => {
if (user.value == 'f' && pass.value == 'f') {
user.value = "";
pass.value = "";
logDiv.style.display = 'none';
wrong.style.display = 'none';
} else {
wrong.style.display = 'block';
}
});

// LOGOUT

const logout = document.getElementById('logout');

logout.addEventListener('click', () => {
if (logDiv.style.display == 'none') {
logDiv.style.display = 'block';
}
});

function resetForm() {
if (logDiv.style.display == 'none') {
user.reset();
pass.reset();
}
}
<div id="loginDiv">
<input id="user" type="text" placeholder="username"></input>
<input id="pass" type="password" placeholder="password"></input>
<button id="logBtn" onclick="resetForm()">login</button>
</div>
<div id="wrong" style="display: none;">
<p id="wrongText">incorrect login info.</p>
</div>

<div id="logoutDiv">
<button id="logout" type="button">logout</button>
</div>

关于JavaScript 带条件重置输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47273367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com