Correction status:qualified
Teacher's comments:
代码:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>3.$.ajax()</title> </head> <body> <h2>用户登录</h2> <form> <p>用户名:<input type="text" name="name"></p> </form> </body> </html> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript"> $(':input').blur(function(){ $.ajax({ url: 'api/demo.php', type: 'GET', // data: 'name='+$(':input').val(), 查询字符串键值对,多个值之间用&连接 // data: {'name':$(':input').val()}, json格式 // data: $('form:first').serialize(), 序列化为查询值对格式 data: $('form:first').serializeArray(), //序列化为json格式 success: function(msg,status,xhr) { $('p span').empty() $('p').append($(msg)) } }) </script>
点击 "运行实例" 按钮查看在线实例
PHP 部分:
<?php $nameList = ['admin','peter','php']; $userName = $_GET['name']; if (strlen(trim($userName))==0) { echo '<span style="color:red">用户名不能为空</span>'; } else if (is_numeric($userName)) { echo '<span style="color:red">用户名不能为纯数字</span>'; } else if (in_array($userName, $nameList)) { echo '<span style="color:red">用户名太抢手了,换一个</span>'; } else { echo '<span style="color:green">恭喜,用户名可用</span>'; } ?>
点击 "运行实例" 按钮查看在线实例
手抄: