对于其中一个项目,我准备了一个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 CODE
<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>
答案 0 :(得分:1)
添加:
setTimeout(function(){
$('#error').hide()
}, 3000) // time in millisecond for as long as you like
答案 1 :(得分:0)
您可以使用setTimeout函数作为条件。将您的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();
//this will hide message after 3 seconds
setTimeout(function(){
$("#error").hide();
},3000)
// Returns successful data to php.
$.post(" ", {name: name, place: place},
function(data)
{
$("#form").append(data);
});
}
}
</script>
答案 2 :(得分:0)
警报无法进行此操作,但您可以使用div创建自己的警报 例如:
function tempAlert(msg,duration)
{
var el = document.createElement("div");
el.setAttribute("style","position:absolute;top:40%;left:20%;background-color:white;");
el.innerHTML = msg;
setTimeout(function(){
el.parentNode.removeChild(el);
},duration);
document.body.appendChild(el);
}
现在使用此自定义提醒
tempAlert("Your message",1000);
上述方法中的数字描述了显示警报的总时间
我希望这能解决你的问题
干杯