用户点提交按钮之后,我想在页面中显示一张图片,几秒钟之后再提交:
<form method="post" action="index.php?ac=exchange" name="exchangefrm" onsubmit="return check_exchange(this);"><script type="text/javascript">
function check_exchange(form) {
form = document.exchangefrm;
if(form.linkman.value.trim() == '') {
alert('未填写联系人。');
return false;
}
else {
if( confirm('您确定联系方式正确并抽取此礼品吗?') ){
document.getElementById('loading').style.display='block';
document.getElementById('message').style.display='none';
setTimeout(function(){ form.submit();}, 5000);
}
else
{
return false;
}
}
</script>
现在的问题是,loading 图片根本没显示5秒,刷一下就过去了用过这个帖子里面的方法,都不能解决问题,要么是能显示5秒loading,却不能正常提交
http://topic.csdn.net/u/20100514/16/2e09281d-6e17-46be-a4c8-7fdd19c7a744.html?seed=933279973&r=65459617#r_65459617
<form method="post" action="index.php?ac=exchange" name="exchangefrm" onsubmit="return check_exchange(this);"><script type="text/javascript">
function check_exchange(form) {
form = document.exchangefrm;
if(form.linkman.value.trim() == '') {
alert('未填写联系人。');
return false;
}
else {
if( confirm('您确定联系方式正确并抽取此礼品吗?') ){
document.getElementById('loading').style.display='block';
document.getElementById('message').style.display='none';
setTimeout(function(){ form.submit();}, 5000);
}
else
{
return false;
}
}
</script>
现在的问题是,loading 图片根本没显示5秒,刷一下就过去了用过这个帖子里面的方法,都不能解决问题,要么是能显示5秒loading,却不能正常提交
http://topic.csdn.net/u/20100514/16/2e09281d-6e17-46be-a4c8-7fdd19c7a744.html?seed=933279973&r=65459617#r_65459617
String.prototype.trim = function() {
return this.replace(/(^\s*)|(\s*$)/g, "");
}忘了这段了
String.prototype.trim = function() {
return this.replace(/(^\s*)|(\s*$)/g, "");
}
是有这段的,忘了贴出了了
function check_exchange(form) {
form = document.exchangefrm;
if(form.linkman.value.trim() == '') {
alert('未填写联系人。');
return false;
}
else {
if( confirm('您确定联系方式正确并抽取此礼品吗?') ){
document.getElementById('loading').style.display='block';
document.getElementById('message').style.display='none';
setTimeout(function(){ form.submit();}, 5000);
return true; // 似乎需要加上这行,否则返回undefined == false,会阻止事件冒泡,导致不会提交
}
else
{
return false;
}
}
</script>
document.getElementById('loading').style.display='block';
document.getElementById('message').style.display='none';
alert("here1");
// 下面这行实际上只能保证5秒后执行submit(),但不能延迟5秒后再显示here2
setTimeout(function(){ form.submit();}, 5000);
alert("here2");// LZ实际上需要一个sleep(5)的效果,但我没有查到这样现成的函数,模拟一个咯
alert( "here0" );
var d = new Date().getTime();
var d1 = d;
while( d1-d < 5000 ) d1 = new Date().getTime(); // 延迟了约5秒时间, 但是会浪费一点CPU
alert("here1");
form.submit(); // 感觉这行应该是不用的。我的习惯是check只做check的事情
return true;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<script type="text/javascript">
function check(obj) {
var _form = document.getElementById('testForm');
var _value = document.getElementById('key').value;
var _flag = false;
if(_value != '') {
_flag = true;
}
if(_flag) {
setTimeout(function(){_form.submit();}, 5000);
}
return false;
}
</script>
<form id='testForm' method="post" action="#testForm" onsubmit="return check()">
<input type="text" name="key" id='key' />
<input type="submit" value="submit" />
</form>
</body>
</html>