< ! DOCTYPE html>
< html lang= "en" >
< head> < meta charset= "UTF-8" > < title> 表单注册< / title> < style> body { margin: 0 ; padding: 0 ; background- color: #F7F7F7 ; } ul { margin: 0 ; padding: 50 px; list- style: none; } . register { width: 800 px; margin: 50 px auto; background- color: #FFF ; border: 1 px solid #CCC ; border- radius: 5 px; } li { display: flex; margin: 20 px 0 ; } label, input { display: block; float: left; height: 46 px; font- size: 24 px; box- sizing: border- box; color: #333 ; } label { width: 200 px; line- height: 46 px; margin- right: 30 px; text- align: right; } input { width: 320 px; padding: 8 px; line- height: 1 ; outline: none; position: relative; } input. code { width: 120 px; } input. verify { width: 190 px; margin- left: 10 px; } input. disabled { background- color: #CCC ! important; } input[ type= button] { border: none; color: #FFF ; background- color: #E64145 ; border- radius: 4 px; cursor: pointer; } . tips { position: fixed; top: 0 ; width: 100 % ; height: 40 px; text- align: center; } . tips p { min- width: 300 px; max- width: 400 px; line- height: 40 px; margin: 0 auto; color: #FFF ; display: none; background- color: #C91623 ; } input. gray { background- color: #ccc; } < / style>
< / head>
< body> < div class = "register" > < form id= "ajaxForm" > < ul> < li> < label for = "" > 用户名< / label> < input type= "text" name= "name" class = "name" > < / li> < li> < label for = "" > 请设置密码< / label> < input type= "password" name= "pass" class = "pass" > < / li> < li> < label for = "" > 请确认密码< / label> < input type= "password" name= "repass" class = "repass" > < / li> < li> < label for = "" > 验证手机< / label> < input type= "text" name= "mobile" class = "mobile" > < / li> < li> < label for = "" > 短信验证码< / label> < input type= "text" name= "code" class = "code" > < input type= "button" value= "获取验证码" class = "verify" > < / li> < li> < label for = "" > < / label> < input type= "button" class = "btn" value= "立即注册" > < / li> < / ul> < / form> < / div> < ! -- 提示信息 -- > < div class = "tips" > < p> aaa< / p>
< / div> < script src= "https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js" > < / script> < script> $ ( '.btn' ) . click ( function ( ) { var str = $ ( '#ajaxForm' ) . serialize ( ) ; $. ajax ( { type: 'post' , url: './register.php' , data: str, dataType: 'json' , beforeSend: function ( ) { if ( $ ( '.name' ) . val ( ) . trim ( ) . length == 0 ) { $ ( '.tips p' ) . fadeIn ( 500 ) . delay ( 1000 ) . fadeOut ( 500 ) . text ( '亲,用户名不能为空哦' ) ; return false ; } if ( $ ( '.pass' ) . val ( ) . length < 6 ) { $ ( '.tips p' ) . fadeIn ( 500 ) . delay ( 1000 ) . fadeOut ( 500 ) . text ( '亲,密码长度不能小于6位哦' ) ; return false ; } if ( $ ( '.pass' ) . val ( ) !== $ ( '.repass' ) . val ( ) ) { $ ( '.tips p' ) . fadeIn ( 500 ) . delay ( 1000 ) . fadeOut ( 500 ) . text ( '亲,两次密码不一致哦' ) ; return false ; } var telReg = /^1\d{10}$/ ; if ( ! telReg. test ( $ ( '.mobile' ) . val ( ) ) ) { $ ( '.tips p' ) . fadeIn ( 500 ) . delay ( 1000 ) . fadeOut ( 500 ) . text ( '亲,手机号有误哦' ) ; return false ; } if ( $ ( '.code' ) . val ( ) . length !== 4 ) { $ ( '.tips p' ) . fadeIn ( 500 ) . delay ( 1000 ) . fadeOut ( 500 ) . text ( '亲,验证码有误哦' ) ; return false ; } $ ( '.btn' ) . addClass ( 'gray' ) . val ( '正在注册...' ) . prop ( 'disabled' , true ) ; } , success: function ( info) { console. log ( info) ; if ( info. code === 200 ) { alert ( info. msg) ; } } , error: function ( ) { } , complete: function ( ) { $ ( '.btn' ) . removeClass ( 'gray' ) . val ( '立即注册' ) . prop ( 'disabled' , false ) ; } } ) ; } ) < / script> < / body>
< / html>