gpt4 book ai didi

HTML 输入表单错误消息闪烁

转载 作者:太空宇宙 更新时间:2023-11-04 10:49:08 25 4
gpt4 key购买 nike

所以我创建了这个非常简单的年龄限制,当用户未满 18 岁时,他们会在“验证年龄”按钮底部收到一条错误消息“再试一次”。一切正常,但当“再试一次”消息显示时,它只会闪烁,显示一秒钟然后消失。我希望它保持可见,直到用户单击其中一个输入框。我究竟做错了什么?如果您在年份输入框中输入 2010,您可以看到“再试一次”消息闪烁。

<html>
<head>
<link href='https://fonts.googleapis.com/css?family=Oswald:400,700' rel='stylesheet' type='text/css'>

<style>

body{
width: 100%;
padding: 0;
margin: 0;
}

#ageGateWrapper{
background: rgba(0,0,0,0.75);
text-align: center;
width: 512px;
height: 288px;
border: 2px solid #d5882c;
}

#innerWrapper{
position: relative;
top: 10%;
}

h1{
position: relative;
font-size: 24px;
font-family: 'Oswald', sans-serif;
font-weight: 400;
color: #ffffff;
padding-bottom: 10px;
letter-spacing: 5px;
}


#ageGateWrapper ul{
list-style: none;
margin: 0 auto;
padding: 0;
}

#ageGateWrapper ul li{
display: inline-block;
}

input {
padding: 10px;
width: 100%;
font-size: 16px;
font-family: 'Oswald', sans-serif;
font-weight: 400;
letter-spacing: 2px;
color: #ffffff;
border: 2px solid #d5882c;
background-color: #000000;
width: 80px;
height: 50px;
text-align: center;
margin-right: 10px;
}

#submit {
position: relative;
top: 15px;
text-align: center;
margin: 0 auto;
color: #ffffff;
font-size: 16;
font-family: 'Oswald', sans-serif;
font-weight: 400;
letter-spacing: 2px;
width: 135px;
height: 60px;
margin: 0;
padding: 0;
background: #000000;
border: 2px solid #d5882c;
}


#errorDiv{
position: relative;
display: none;
top:25px;
font-size: 16;
font-family: 'Oswald', sans-serif;
font-weight: 400;
color: #ffffff;
}

</style>

<script language="javascript">

function age(){
var validated = false;
var minimumAge = 18;
var minYear;
var bday=parseInt(document.forms[0].txtBday.value);
var bmo=(parseInt(document.forms[0].txtBmo.value)+1);
var byr=parseInt(document.forms[0].txtByr.value);
//var byr;
var age;
var now = new Date();
currentDay=now.getDate();
currentMo=(now.getMonth()+1);
currentYr=(now.getFullYear());
console.log("tday " + currentDay);
console.log("tmo " + currentMo);
console.log("tyr " + currentYr);

minYear = currentYr - minimumAge;




if(byr > minYear){
failed();
}


if(byr >= minYear){

if(bmo == currentMo){

if(bday >= currentDay){
//alert("PASS");
passed();
}

}else if(bmo < currentMo){
failed();

}else if(bmo > currentMo){
//alert("PASS 2");
passed();
}
}

}



function passed(){
alert("SHOW CONTENT");
window.parent.postMessage("teaser ended", '*');
}

function failed(){
document.getElementById("errorDiv").style.display= "block";
}

</script>
</head>
<body>
<div id="ageGateWrapper">
<div id="innerWrapper">
<h1> ENTER YOUR DATE OF BIRTH</h1>
<form>
<ul>
<li><div> <input type="text" name="txtBmo" placeholder="MM"> </div></li>
<li><div> <input type="text" name="txtBday" size="4" placeholder="DD"> </div></li>
<li><div> <input type="text" name="txtByr"size="4" placeholder="YYYY"> </div></li>
<ul>
<input type="submit" value="VERIFY AGE" onClick="age()" id="submit">
<div id="errorDiv">Try Again </di>

<form>
</div>
</div>

</body>
</html>

最佳答案

您应该通过提供事件参数并对其应用 preventDefault() 方法来阻止表单提交:

html

<input type="submit" value="VERIFY AGE" onClick="age(event)" id="submit">

javascript

function age(event){
event.preventDefault();
var validated = false;
...
}

关于HTML 输入表单错误消息闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35121471/

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