Blogger Information
Blog 60
fans 0
comment 1
visits 37613
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
4月10日作业$.ajax()方法操作
威灵仙的博客
Original
515 people have browsed it

代码:

实例

<!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>';
}

?>

运行实例 »

点击 "运行实例" 按钮查看在线实例

手抄:

303857417.jpg

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post