实时验证用户名是否重复-创新互联
现在很多网站在注册用户名时,你只要输入好要注册的用户名后,不用提交请求就可以实时的显示用户名是否可用,重复等。这总情况下不能刷新页面,而要把用户名提交到后台比对,后台把结果返回到前端,前端再显示出用户名是否可用。下面将使用JQuery的ajax方法,和JQuery的表单功能来实现我们的需求

创新互联专注于企业
成都全网营销推广、网站重做改版、崇州网站定制设计、自适应品牌网站建设、
H5场景定制、
商城建设、集团公司官网建设、外贸营销网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为崇州等各大城市提供网站开发制作服务。
JQuery ajax方法
此方法可以提交任何地方的数据到后台,实现代码如下
在这之前需要加载JQuery的js
$(function () {
var old_data = $("#id_ServerName").val();
$("#id_ServerName").change(function () {
var data = $("#id_ServerName").val();
if (data !== old_data){
$.ajax({
url: "/am/check",
type: "post",
data: {"host_name": data},
success: function (arg) {
if (arg === "Error"){
$("#id_error_info").removeClass("error_info");
}else if (arg === "OK") {
$("#id_error_info").addClass("error_info");
}else{
console.log("OK");
}
},
fail: function () {
}
})
}else {
$("#id_error_info").addClass("error_info");
}
})
});实现思路:
首先是创建一个自运行函数
然后定义一个old_data,获取初始值,根据业务不同,这里不一定大家都需要,如果是注册,判断用户名是否存在就不需要,但如果是修改用户名,就需要,因为修改用户名的时候,首先需要获取到当前用户名
然后再通过JQuery获取id为id_ServerName的标签(通常为input标签),并监听一个change事件(当value发生更改时)执行一个函数
获取新的值(这个值是需要传入后台进行比对的数据)
判断新值是否等于老的值,如果等于就不进行任何操作,我这里是添加了一个类,这个类是用于将错误信息隐藏。如果不等于,就需要执行ajax将数据传入后台,然后后台进行比对
ajax指定了需要传入的url(这里分为本域请求和跨域请求,我这里是本域请求,如果是跨域自行百度解决)。指定数据传输类型为post,也可以指定为get。指定了传入的数据,数据为一个字典,key为自定义,value为获取到的新数据,后端接收到就为字典。seccess,定义了一个函数,这个函数用来处理请求成功后端返回的数据,这个数据用了一个参数去接收,参数名自定义,我这里为arg。Error,OK为后端处理好数据后返回的结果,前端根据返回结果做处理。fail也定义了一个函数,这个函数用来处理请求失败时的处理函数,比如超时或者其它
JQuery 表单验证
此方法可提交form表单内指定的数据到后台,此外表单功能对ajax做了一些封装,使其使用更简单,更方便,推荐使用,代码如下
首先需要加载JQuery表单验证的模块
基本
文件
流程
错误
SQL
调试
- 请求信息 : 2026-05-19 11:30:29 HTTP/1.1 GET : /article/ccdpso.html
- 运行时间 : 0.7040s ( Load:0.0065s Init:0.0011s Exec:0.6869s Template:0.0094s )
- 吞吐率 : 1.42req/s
- 内存开销 : 2,239.13 kb
- 查询信息 : 12 queries 5 writes
- 文件加载 : 36
- 缓存信息 : 0 gets 0 writes
- 配置加载 : 130
- 会话信息 : SESSION_ID=46o2r5rgfuckir3ha0qfsc8kd1
- /www/wwwroot/tsicrk.com/index.php ( 1.09 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/ThinkPHP.php ( 4.61 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Think.class.php ( 12.26 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Storage.class.php ( 1.37 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Storage/Driver/File.class.php ( 3.52 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Mode/common.php ( 2.82 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Common/functions.php ( 53.56 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Hook.class.php ( 4.01 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/App.class.php ( 13.49 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Dispatcher.class.php ( 14.79 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Route.class.php ( 13.36 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Controller.class.php ( 11.23 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/View.class.php ( 7.59 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/BuildLiteBehavior.class.php ( 3.68 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ParseTemplateBehavior.class.php ( 3.88 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ContentReplaceBehavior.class.php ( 1.91 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Conf/convention.php ( 11.15 KB )
- /www/wwwroot/tsicrk.com/App/Common/Conf/config.php ( 2.14 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Lang/zh-cn.php ( 2.55 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Conf/debug.php ( 1.49 KB )
- /www/wwwroot/tsicrk.com/App/Home/Conf/config.php ( 0.31 KB )
- /www/wwwroot/tsicrk.com/App/Home/Common/function.php ( 3.33 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior.class.php ( 5.62 KB )
- /www/wwwroot/tsicrk.com/App/Home/Controller/ArticleController.class.php ( 6.02 KB )
- /www/wwwroot/tsicrk.com/App/Home/Controller/CommController.class.php ( 1.60 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Model.class.php ( 60.11 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php ( 32.43 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db/Driver/Pdo.class.php ( 16.74 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Cache.class.php ( 3.83 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Cache/Driver/File.class.php ( 5.87 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template.class.php ( 28.16 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template/TagLib/Cx.class.php ( 22.40 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template/TagLib.class.php ( 9.16 KB )
- /www/wwwroot/tsicrk.com/App/Runtime/Cache/Home/7540f392f42b28b481b30614275e4e55.php ( 17.71 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/WriteHtmlCacheBehavior.class.php ( 0.97 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ShowPageTraceBehavior.class.php ( 5.24 KB )
- [ app_init ] --START--
- Run Behavior\BuildLiteBehavior [ RunTime:0.000005s ]
- [ app_init ] --END-- [ RunTime:0.000034s ]
- [ app_begin ] --START--
- Run Behavior\ReadHtmlCacheBehavior [ RunTime:0.000302s ]
- [ app_begin ] --END-- [ RunTime:0.000326s ]
- [ view_parse ] --START--
- [ template_filter ] --START--
- Run Behavior\ContentReplaceBehavior [ RunTime:0.000056s ]
- [ template_filter ] --END-- [ RunTime:0.000078s ]
- Run Behavior\ParseTemplateBehavior [ RunTime:0.006492s ]
- [ view_parse ] --END-- [ RunTime:0.006518s ]
- [ view_filter ] --START--
- Run Behavior\WriteHtmlCacheBehavior [ RunTime:0.000156s ]
- [ view_filter ] --END-- [ RunTime:0.000171s ]
- [ app_end ] --START--
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`pid`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( pid= )
- [8] Undefined index: pid /www/wwwroot/tsicrk.com/App/Home/Controller/ArticleController.class.php 第 47 行.
- [8] Undefined index: db_host /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 120 行.
- [8] Undefined index: db_port /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 121 行.
- [8] Undefined index: db_name /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 122 行.

0.7040s
