jquery 获取当前对象的id取巧验证的一种方法
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>title</title>
<script src="jquery-1.8.3.min.js"></script>
<style>
.tips {
display:none;
color: white;
font-size: 13px;
}
.red{
color:red;
}
</style>
</head>
<body>
<div style="height:100%;weight:900px; background-color:#2A211C" >
<form method="post"action="">
<input type="text" class="ipt" name="db_prefix" id="db_prefix" value="wst_" onblur="checkVal(this.id)">
<span class="tips" style="display:inline-block "> (建议修改表前缀)</span>
<input type="text" class="ipt" name="db_host" id="db_host" value="localhost" onblur='checkVal(this.id)'>
<span class='db_host tips'>数据库主机不能为空</span><br />
<input type="password" class="ipt" name="admin_password" id="admin_password" value="" onblur='checkVal(this.id)'>
<span class='admin_password tips'>管理员密码不能为空</span>
</form>
</div>
</body>
<script>
function checkVal(name){
<!-- console.log($('#'+name).val()); -->
if( $('#'+name).val() == ''){
$('.'+name).show().addClass('red');
}else{
$('.'+name).hide().removeClass('red');
}
}
</script>
</html>
注意:这种方法该对象的id和该提示信息的class的值必须相同!!

浙公网安备 33010602011771号