<body> <form action="php/test.php" method="post"> 用户名:<input type="text" name="username" id="username" value="" /><span></span> 密码:<input type="password" name="pwd" id="pwd" value="" /><span></span> <input type="button" value="提交按钮" id="submit"/> </form> <script src="js/jquery-2.2.1.min.js"></script> <script> $("#username").blur(function(){ $.ajax({ type:"post", url:"json1.json", async:true, success:function(data){ var aDate = eval(data); var arrUser = []; for(var k in data.username)//遍历json1文件中的 username { arrUser.push(data.username[k].name);//找到每个键名下的键值 // } // console.log(arrUser);//检查数组 // 遍历数组 for(var i = 0 ; i < arrUser.length;i++) { if( $("#username").val() == arrUser[i] ) { alert("用户名已被注册"); }else{ alert("用户名没有被注册"); } } } }); })
json1.json
{ "username" : [ {"name":"mingjixiaohui"}, {"name":"jingdong"} ], "password" :[ {"pwd":"123"}, {"pwd":"456"} ] }
用户注册界面 ,ajax检查用户名是不是被注册过