<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>验证码实验</title>
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
<style>
div{ width:100px; height:20px; background:#ccc; overflow:hidden; transition:all .5s ease-in .1s;}
p{ margin:0; padding:0; display:inline;}
p:hover{cursor:pointer;}
#dome{ background:#0f0; padding:3px; font-weight:bold;letter-spacing:2px}
div:hover{ height:50px;}
</style>
<body>
<form>
<p id="dome"></p> <p id="refresh">刷新</p>
<input type="text" id="yzm" autofocus>
<input type="submit" value="提交验证码" id="sub" >
</form>
<script>
var nub=['0','A','B','1','C','D','3','E','F','4','G','H','5','I','J','6','K','L','7','M','N','8','O','P','9','Q','R','S','T','U','V','W','X','Y','Z'];
var y;
function yzm(){
var k="";
for(var i=0;i<4;i++){
var j=Math.ceil(Math.random()*34);
var k=k+nub[j];
}
$('#dome').text(y=k);
}
$('#sub').click(function(){
if($('#yzm').val().toUpperCase()!=y){
alert("验证码错误!");
}
else{alert("验证码正确");}
})
$("#refresh,#dome").click(function(){
yzm()
$('#yzm').val("");
})
yzm()
</script>
</body>
</html>为什么每次点“提交验证码“时左边的”验证码“都会被刷新一次?
3 回答
已采纳
woshiajuana
TA贡献211条经验 获得超152个赞
因为你的按钮<input type="submit" value="提交验证码" id="sub" > 的type是submit方式提交的
这种方式默认提交后刷新页面,你可以把这个改成<input type="button" value="提交验证码" id="sub" >
添加回答
举报
0/150
提交
取消