gpt4 book ai didi

javascript - 一段时间后消息消失

转载 作者:行者123 更新时间:2023-12-02 00:50:39 24 4
gpt4 key购买 nike

对于其中一个项目,我准备了一个 HTML 表单并且代码运行良好。我需要的是错误或成功消息应在几秒钟后消失。任何机构都可以帮助我更正代码。代码如下。

HTML 格式

<div class="form">
<form id="form" name="form" method="post" action=" ">
<div id="fc">
<div id="error"></div>
<label>Name: </label>
<input type="text" name="name" id="name" />
<label>Place: </label>
<input type="text" name="name" id="place" />
</div>
<div id="btn">
<input type="submit" id="submit" value="SUBMIT" onClick="Submit(event)" />
</div>
</form>
</div>

CSS 代码

.form{ margin:0 auto;width:280px;
padding:10px;border:solid 2px #b7ddf2;
background:#f5fffa;
}
#fc{
font-family:"Lucida Grande", "Lucida Sans Unicode",
Verdana, Arial, Helvetica, sans-serif; font-size:12px;
}

#fc label{display:block;font-weight:bold;
text-align:right; width:120px;float:left;
font-size:14px;
}

#fc input{float:left;font-size:12px;
padding:3px 3px; border:solid 1px #aacfe4;
width:130px; margin:2px 0 10px 10px;
}

#btn{clear:both;font-size:13px;font-weight:bold;
text-align:center; margin-bottom: 2px;
}
#error{ font-weight:bold;
text-align:left;
height: 20px;
color: red;font-size:15px;
}

JS 代码

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script>
function Submit(e){
e.preventDefault();

var name = $("#name").val();
var place = $("#place").val();

if($("#name").val() == "" ){
$("#name").focus();
$("#error").html("Enter the Name.");
return false;
}
else if($("#place").val() == "" ){
$("#place").focus();
$("#error").html("Enter the Place.");
return false;
}

else if($(name != '' && place != '' )){
$("#error").html("Form submitted successfully.")
$("#form")[0].reset();

// Returns successful data to php.
$.post(" ", {name: name, place: place},
function(data)
{
$("#form").append(data);
});
}
}
</script>

最佳答案

添加:

setTimeout(function(){
$('#error').hide()
}, 3000) // time in millisecond for as long as you like

引用:http://www.w3schools.com/jsref/met_win_settimeout.asp

关于javascript - 一段时间后消息消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40277835/

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