jQuery验证+ AJAX电子邮件可用性检查 [英] jQuery validate + AJAX e-mail availiability check
问题描述
已更新 我正在使用jQuery validate插件和远程方法来检查ajax的电子邮件可用性,但是我不明白为什么ajax只调用一次.输入电子邮件后,ajax会拨打电话,并在控制台中打印输入的电子邮件,但是如果我删除它并写另一封电子邮件,则不会发生任何事情.
UPDATED I'm using jQuery validate plugin and remote method to check email availiability with ajax, but I don't understand why ajax calls only once. When email is entered ajax makes call and in console prints email that is entered, but if i delete it and write in another email nothing happens.
HTML
<input type="text" name="on0" value="Your e-mail" class="form-field no-mr check-email">
jQuery
jQuery.validator.addMethod('defaultInvalid', function(value, element) {
return !(element.value == element.defaultValue);
}, 'Custom Error Message for this field');
$('.checkout-form').validate({
rules: {
on0 : 'required defaultInvalid email'
}
});
$('.check-email').rules("add", {
"remote" : {
url: 'c/',
type: "post",
dataType: 'html',
data: {
check: function() {
return $('.check-email').val();
}
},
success: function(data){
console.log(data);
},
error: function(data){
console.log(data);
}
}
});
ajax发布帖子的PHP
PHP where ajax makes post
<?php
echo $_POST['check'];
?>
推荐答案
ajax只触发一次的原因是,您覆盖了jQuery validate的远程方法的成功回调.保持简单,然后尝试如下操作:
the reason why ajax fires only once is that you have overwritten the success callback of jQuery validate's remote method. Keep it simple and try something like this:
<input name="email_address"/>
带脚本
<script>
$(function () {
$('form').validate({
rules: {
email_address: {
required:true,
remote: {
url: "your_script.php"
}
}
},
debug: true,
submitHandler: function () { alert('ok'); }
});
});
</script>
和php之类的
<?php
echo $_GET['email_address'] == "bob";
?>
这篇关于jQuery验证+ AJAX电子邮件可用性检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!