用一个例子来说明:
html部分
<form action="name1">
<input class="class1" type="text" placeholder="用户名" id="userName" name="userName"><span id="span1">用户名输入有误</span><span id="span2">此用户名可用</span><br>
<input class="class1" type="password" placeholder="密码"><br>
<input class="class2" type="button" value="登录">
<input class="class2" type="button" value="取消">
</form>
js部分
$("#userName").blur(function(){ $.ajax({ url:"aa.php", type:"POST", data:{"userName":$('#userName').val()}, dataType:"html", success:function(data){ if(data=="1"){ $("#span1").css({"display":"block"}); } if(data=="0"){ $("#span2").css({"display":"block"}); } } }) })
php部分
<?php header("content-type:text/html;charset=utf-8"); $userName=$_POST['userName']; if($userName="123456"){ echo "1"; }else{ echo "0"; } ?>