jquery+ajax+php简单示例
2015-09-15 18:19
751 查看
index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style> *{ margin:0px; padding:0px; } .main{text-align:center;width:1000px;margin:auto;} .main form{margin-top:30px;} .main input{width:200px;height:36px;} .main table tr th{width:100px;height:56px;border:1px solid black;} .main table tr td{width:300px;height:56px;border:1px solid black;} .tip_more{color:black;} .tip_erro{color:red;} .tip_right{color:green;} </style> <script type="text/javascript" src="js/jquery-1.8.3.min.js"></script> <script type="text/javascript"> function onFocus(input){ switch(input.id){ case 'name': $('#'+input.id+'_p').html("*<span>此处填写用户名</span>"); $('#'+input.id+'_p').attr("class", "tip_more"); break; case 'age': $('#'+input.id+'_p').html("*<span>此处填写年龄</span>"); $('#'+input.id+'_p').attr("class", "tip_more"); break; } } function onBlur(input){ switch(input.id){ case 'name': //alert(input.value.trim()); if(input.value.trim()==''){ $('#'+input.id+'_p').html("*<span>请输入用户名</span>"); $('#'+input.id+'_p').attr("class", "tip_erro"); } else { $('#'+input.id+'_p').html("*<span>正确</span>"); $('#'+input.id+'_p').attr("class", "tip_right"); } break; case 'age': //alert(input.value.trim()); if(input.value.trim()==''){ $('#'+input.id+'_p').html("*<span >请输入用户年龄</span>"); $('#'+input.id+'_p').attr("class", "tip_erro"); } else { $('#'+input.id+'_p').html("*<span >正确</span>"); $('#'+input.id+'_p').attr("class", "tip_right"); } break; } } //$(function(){ $(document).ready(function(){ $("#check").click(function(){ var name=$('#name').val(); //alert(name); $.ajax({ url: "check.php", //请求的url地址 dataType: "json", //返回格式为json async: true, //请求是否异步,默认为异步,这也是ajax重要特性 data: { "name": name }, //参数值 type: "POST", //请求方式 beforeSend: function() { //请求前的处理 }, success: function(data) { if (data.success) { alert(data.msg); } else { alert(data.msg); } }, complete: function() { //请求完成的处理 }, error: function(e) { //请求出错处理 alert('ajax hava an error'+e.status); } }); }); }); </script> </head> <body> <div class="main"> <h1>表单demo</h1> <form method="post" id="apply_form" action="list.php"> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <th>用户 </th> <td> <input id="name" onblur="onBlur(this);" onfocus="onFocus(this);" type="text" name="name"/> </td> <td> <input id="check" type="button" name="check" value="检测用户名"/> </td> <td class="padding3"><p id='name_p' ></p></td> </tr> <tr> <th>年龄 </th> <td> <input id="age" onblur="onBlur(this);" onfocus="onFocus(this);" type="text" name="age"/> </td> <td class="padding3"><p id='age_p' ></p></td> </tr> </table> </form> </div> </body> </html>
check.php <?php $name=$_POST['name']; if($name =="hello") { echo '{"success":false,"msg":"用户名重复"}'; } else{ echo '{"success":true,"msg":"可以使用"}'; } ?>
相关文章推荐
- jQuery 实现侧边浮动导航菜单效果
- jquery-选择器
- JQuery通过AJAX从后台获取信息显示在表格上并支持行选中
- jQuery记录--操作DOM元素.
- jQuery记录--表单选择器
- jQuery记录--普通元素过滤选择器
- jQuery记录--基础选择器
- jquery 批量删除前台jsp 加js加后台 spring
- jQuery实现图片轮播特效代码分享
- jQuery 中post 、get的同步问题
- jquery 选择器选出被选中的input checkbox
- Jquery 2个数组,去除重复的项目
- jQuery 1.9不支持$.browser 怎么判断浏览器类型和版本
- jQuery实现的淡入淡出二级菜单效果代码
- jQuery带进度条全屏图片轮播特效代码分享
- jquery 设置select 默认值
- jquery easyui 实现级联效果
- jquery处理html格式的数据
- 获取当前日期
- jquery attr()方法