我想提交表单使用jquery验证和ajax.But它does not work.neither它会引发任何错误。Jquery验证提交处理程序不工作与Ajax
下面是HTML
{% if form %}
<form method="POST" action="." id="change_password_form">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" name="action" value="{% trans 'change password' %}"/>
</form>
{% else %}
<p>{% trans 'Your password is now changed.' %}</p>
{% endif %}
这里是脚本
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js"></script>
<script type="text/javascript">
$(function() {
$("#change_password_form").validate({
rules: {
password1: {
required: true,
maxlength:10
},
password2: {
required: true,
maxlength:10
},
},
messages: {
password1: {
required: "Please provide a password",
},
password2: {
required: "Please Confirm your password",
},
},
submitHandler: function(form) {
var pass = $('input[name=password1]').val();
var repass = $('input[name=password2]').val();
if (pass != repass) {
console.log("error")
}
else {
$.ajax({
type: $(form).attr('method'),
url: $(form).attr('action'),
data: $(form).serialize(),
})
.done(function (response) {
$('#loading-image').hide();
if (response.location == 'accounts/password/reset/key/done/'){
window.location = "{% url 'home' %}"
}
}).error(function(response){
console.log(response)
});
return false;
}
});
});
</script>
不知何故提交事件没有发生?我能做些什么来提交表单使用Ajax和jQuery验证
我可以显示您的完整解决方案:html + javascript? –
你的'submitHandler'里应该实际提交表单。在validate()方法旁边,你可以听取表单提交'$(form).on('submit',function(){// do ajax submit}'并且执行ajax提交。 –
更新了问题@Amani –