简体   繁体   English

在设计用户身份验证之前,preventDefault

[英]preventDefault before devise User authenticate

i have in my index a modal with user sign up form. 我在索引中有一个带有用户注册表格的模式。 i create a javascript when the buttom save has click on. 当按钮保存已单击时,我创建了一个javascript。 when the users have errors the alerts appear in the modal but it redirect other page. 当用户有错误时,警报会显示在模式中,但会重定向其他页面。

i would the page not redirect when users have errors. 当用户有错误时,我不会页面重定向。

this is the javascript and ajax. 这是javascript和ajax。

 $("#update").click(function(e){ if(!validarEmail()){ $("#divalerta").addClass("alert-danger"); $("#divalerta").removeClass("alert-success"); var j = $("<p id=\\"alerta\\"><span class=\\"glyphicon glyphicon-exclamation-sign\\"></span>Formato de email incorrecto</p>"); mostrarAlerta(j); e.preventDefault(); return; } if($("#newpass").val() != $("#retypepass").val()){ $("#divalerta").addClass("alert-danger"); $("#divalerta").removeClass("alert-success"); var j = $("<p id=\\"alerta\\"><span class=\\"glyphicon glyphicon-exclamation-sign\\"></span>Las contraseñas no concuerdan</p>"); mostrarAlerta(j); e.preventDefault(); return; } var datos = {}; datos['accion'] = "ingresar"; datos['nombre'] = $("#nombre").val(); datos['apellido'] = $("#apellido").val(); datos['email'] = $("#email").val(); datos['pass'] = $("#newpass").val(); //var params={'datos' : datos}; $.ajax({ method: "POST", async: false, data: datos, url: "/", success: function(response){ var arr = response.split("-"); if(arr[0]=="ok"){ $("#divalerta").removeClass("alert-danger"); $("#divalerta").addClass("alert-success"); $("#update").prop('disabled',true); } else{ $("#divalerta").addClass("alert-danger"); $("#divalerta").removeClass("alert-success"); } var j = $("<p id=\\"alerta\\"><span class=\\"glyphicon glyphicon-exclamation-sign\\"></span>"+arr[1]+".</p>"); mostrarAlerta(j); } }); }); 

You could do something like this 你可以做这样的事情

 <form onsubmit="return validator()" var validator = function(){ if(!validarEmail()){ $("#divalerta").addClass("alert-danger"); $("#divalerta").removeClass("alert-success"); var j = $("<p id=\\"alerta\\"><span class=\\"glyphicon glyphicon-exclamation-sign\\"></span>Formato de email incorrecto</p>"); mostrarAlerta(j); return false; } if($("#newpass").val() != $("#retypepass").val()){ $("#divalerta").addClass("alert-danger"); $("#divalerta").removeClass("alert-success"); var j = $("<p id=\\"alerta\\"><span class=\\"glyphicon glyphicon-exclamation-sign\\"></span>Las contraseñas no concuerdan</p>"); mostrarAlerta(j); return false; } var datos = {}; datos['accion'] = "ingresar"; datos['nombre'] = $("#nombre").val(); datos['apellido'] = $("#apellido").val(); datos['email'] = $("#email").val(); datos['pass'] = $("#newpass").val(); //var params={'datos' : datos}; $.ajax({ method: "POST", async: false, data: datos, url: "/", success: function(response){ var arr = response.split("-"); if(arr[0]=="ok"){ $("#divalerta").removeClass("alert-danger"); $("#divalerta").addClass("alert-success"); $("#update").prop('disabled',true); } else{ $("#divalerta").addClass("alert-danger"); $("#divalerta").removeClass("alert-success"); } var j = $("<p id=\\"alerta\\"><span class=\\"glyphicon glyphicon-exclamation-sign\\"></span>"+arr[1]+".</p>"); mostrarAlerta(j); } }); return false; }); 

That way, the form will never reload. 这样,表单将永远不会重新加载。 You can modify this how you so choose, but just know that having an onsubmit attribute return false will not allow the form to reload. 您可以按照自己的选择修改它,但是只知道具有onsubmit属性返回false不会允许重新加载表单。 I hope this helps. 我希望这有帮助。

用return false代替return,它将成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM