我使用以下脚本来验证注册表单:$(document).ready(function() { $("#register").click(function() { var name = $("#name").val(); var email = $("#email").val(); var password = $("#password").val(); var cpassword = $("#cpassword").val(); if (name == '' || email == '' || password == '' || cpassword == '') { alert("Please fill all fields...!!!!!!"); } else if ((password.length) < 8) { alert("Password should at least 8 character in length...!!!!!!"); } else if (!(password).match(cpassword)) { alert("Your passwords don't match. Try again?"); } else { $.post("register.php", { name1: name, email1: email, password1: password }, function(data) { if (data == 'You have Successfully Registered.....') { alert("Tu cuenta de usuario ha sido creada"); } }); } });});我正在检查所有可能的选项,并且我已检测到条件正在运行,但是在满足一个条件后页面会重新加载,而不是停止执行脚本。例如,如果密码长度为6个字符,则警告:alert("Password should at least 8 character in length...!!!!!!");抛出,但页面重新加载,输入字段中的所有文本都被删除...并且当执行$ .post调用并且响应是预期时,警报:alert("Tu cuenta de usuario ha sido creada");没有显示。记录在数据库中创建,页面重新加载,但不显示警报。
4 回答
沧海一幻觉
TA贡献1824条经验 获得超5个赞
您需要在click事件结束时返回false
$(document).ready(function() {$("#register").click(function() { var name = $("#name").val(); var email = $("#email").val(); var password = $("#password").val(); var cpassword = $("#cpassword").val(); if (name == '' || email == '' || password == '' || cpassword == '') { alert("Please fill all fields...!!!!!!"); } else if ((password.length) < 8) { alert("Password should at least 8 character in length...!!!!!!"); } else if (!(password).match(cpassword)) { alert("Your passwords don't match. Try again?"); } else { $.post("register.php", { name1: name, email1: email, password1: password }, function(data) { if (data == 'You have Successfully Registered.....') { alert("Tu cuenta de usuario ha sido creada"); } }); } return false; // put this it will solve your problem});});
添加回答
举报
0/150
提交
取消