gpt4 book ai didi

javascript - 如果文本框为空,则返回 true 或显示确认

转载 作者:行者123 更新时间:2023-11-28 04:43:40 25 4
gpt4 key购买 nike

如果文本框为空,返回true
否则,显示确认

工作测试:http://next.plnkr.co/edit/KfD3oznkl3eBXgPx

我正在尝试做的事情:

当用户输入尝试点击“提交”并且密码文本框为空时,应该直接返回true,但如果密码文本框有数据,则应该显示确认提示。

我做了什么:

当password textfield为空时,直接返回true,当有值时,显示确认,但即使我在确认框中点击“否”,它也返回true

代码:

<script>
function validateForm(e) {
var password=document.getElementById("password").value;
if (password==""){
return true;
}
else{
confirm("Are You Sure you wanted to change password?");
}
}
</script>
<h1> Edit Profile </h1>
<form onSubmit="return validateForm()">
<input type="text" placeholder="Username" id="uname"><br>
<input type="password" placeholder="password" id="password"><br>
<input type="text" placeholder="email" id="email"><br>
<input type="submit" id="check" value="OK">
</form>

最佳答案

Window.confirm()

Returns a boolean value indicating whether OK (true) or Cancel (false) was selected. If a browser is ignoring in-page dialogs, then result is always false.

您应该检查confirm返回的值

function validateForm(e) {
var password=document.getElementById("password").value;
if (password==""){
return true;
}
else{
var confirmRes = confirm("Are You Sure you wanted to change password?");
return confirmRes;
//OR Simply
//return confirm("Are You Sure you wanted to change password?");
}
}
<h1> Edit Profile </h1>
<form onSubmit="return validateForm()">
<input type="text" placeholder="Username" id="uname"><br>
<input type="password" placeholder="password" id="password"><br>
<input type="text" placeholder="email" id="email"><br>
<input type="submit" id="check" value="OK">
</form>

关于javascript - 如果文本框为空,则返回 true 或显示确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58212371/

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