廢話不多說了,直接給大家貼代碼了,具體代碼如下所示:
<script type="text/JavaScript"> //獲取url中的參數 $(function(){ var userName = window.location.href; var aa = userName.indexOf("="); console.log(userName); if (aa == -1) return ""; userName=userName.substring(aa+1); //給hidden賦值並顯示 $("#userName").val(userName).attr("type","text"); //ajax請求修改密碼 var rePassword = $("#repeatPassword").val(); //6到15位字母或數字 var reg=/^[0-9a-zA-Z]{6,15}$/; //新密碼校驗 $("#newPassword").blur(function(){ $("#msg").css("display","none"); var newPassword = $("#newPassword").val(); if(newPassword==""||newPassword==null){ $("#msg").html("請輸入新密碼!").css("display","block"); }elseif(!reg.test(newPassword)){ $("#msg").html("密碼格式不正確,請重新輸入!").css("display","block"); } }); //重復密碼校驗 $("#repeatPassword").blur(function(){ var newPass = $("#newPassword").val(); var repPass = $("#repeatPassword").val(); if(newPass != repPass){ $("#msg").html("兩次輸入密碼不一致,請重輸!").css("display","block"); } }); //表單提交前校驗 $("#updateBtn").click(function(){ var flag = true; var newPass = $("#newPassword").val(); var repPass = $("#repeatPassword").val(); var reg1=/^[0-9a-zA-Z]{6,15}$/; if(!reg1.test(newPass)||newPass!=repPass||newPass ==""||newPass==null){ flag = false; } if(flag){ $.ajax({ url:"/HuaMuWebsite/userManage/editPassword?userName="+userName, type:"POST", async:false, data:$("#fm").serialize(), //表單數據序列化, 可以對form表單進行序列化,從而將form表單中的所有參數傳遞到服務端。 success:function(data){ //json字符串轉為json對象 var jsonObj=eval("("+data+")"); if(jsonObj.success != null){ alert(jsonObj.success); window.location.href="/HuaMuWebsite/admin/login.html" rel="external nofollow" ; }else{ alert(jsonObj.failure); } }, error:function(e){ alert("請求出錯!"); } }); } }); }); </script>