
跪求各位js高手,为什么设置了正则表达式,鼠标移出文本框没有提示内容???
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/...
<!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>
<style>
#span3 {
width:100px;
height:30px;
font-size:12px;
display:none;
position:absolute;
background:green;
}
#span2 {
width:100px;
height:30px;
font-size:12px;
display:none;
position:absolute;
background:red;
}
#span {
width:100px;
height:30px;
display:none;
font-size:12px;
position:absolute;
background:#990;
}
</style>
</head>
<body>
<form id="personReg" method="post" onsubmit="return false">
用户名:<input type="text" id="txt" name="txt"/>
<span id="span">请输入密码:2-20个非空字符</span>
<span id="span2">密码不合法,请重新输入</span>
密 码:<input type="password" id="pwd" name="pwd"/>
</form>
<script type="text/javascript" src="www/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(function(){
var value=$.trim($(this).val());
$("#personReg #txt").bind('focus',function(){
}).bind('blur',function(){
if(value=="")
{
$("#span").css('display','none');
$("#span2").css('display','block');
}
else if(!/[a-zA-Z0-9_]{2,20}/.test(value))
{
$("#span").css('display','block');
$("#span2").css('display','none');
}
})
})
</script>
</body>
</html> 展开
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>获得表单的值</title>
<style>
#span3 {
width:100px;
height:30px;
font-size:12px;
display:none;
position:absolute;
background:green;
}
#span2 {
width:100px;
height:30px;
font-size:12px;
display:none;
position:absolute;
background:red;
}
#span {
width:100px;
height:30px;
display:none;
font-size:12px;
position:absolute;
background:#990;
}
</style>
</head>
<body>
<form id="personReg" method="post" onsubmit="return false">
用户名:<input type="text" id="txt" name="txt"/>
<span id="span">请输入密码:2-20个非空字符</span>
<span id="span2">密码不合法,请重新输入</span>
密 码:<input type="password" id="pwd" name="pwd"/>
</form>
<script type="text/javascript" src="www/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(function(){
var value=$.trim($(this).val());
$("#personReg #txt").bind('focus',function(){
}).bind('blur',function(){
if(value=="")
{
$("#span").css('display','none');
$("#span2").css('display','block');
}
else if(!/[a-zA-Z0-9_]{2,20}/.test(value))
{
$("#span").css('display','block');
$("#span2").css('display','none');
}
})
})
</script>
</body>
</html> 展开
1个回答
展开全部
你的blur绑定的是用户名的input,不是密码的。改成这样:
<script type="text/javascript">
$(function () {
var value = $.trim($(this).val());
$("#personReg #pwd").bind('focus', function () {}).bind('blur', function () {
if (value == "") {
$("#span").css('display', 'none');
$("#span2").css('display', 'block');
} else if (!/[a-zA-Z0-9_]{2,20}/.test(value)) {
$("#span").css('display', 'block');
$("#span2").css('display', 'none');
}
})
})
</script>
更多追问追答
追问
这样写,好像正则表达式还是没有起作用
追答
你的正则表达式没有问题,问题仍然是var value = $.trim($(this).val());放错地方了。改成这样:
<script type="text/javascript">
$(function () {
$("#personReg #pwd").bind('focus', function () {}).bind('blur', function () {
var value = $.trim($(this).val());
if (value == "") {
$("#span").css('display', 'none');
$("#span2").css('display', 'block');
} else if (!/[a-zA-Z0-9_]{2,20}/.test(value)) {
$("#span").css('display', 'block');
$("#span2").css('display', 'none');
}
})
})
</script>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询