看起来很简单,但我无法得到解决方案。 serialize()没有获得任何表单值。 console.log(data)在控制台中检索空文本。
表格
<form id="form-contact" novalidate="novalidate" method="post" action="#" role="form">
<fieldset class="col-xs-5">
<div class="form-group">
<label for="firstname">First name <span>*</span></label>
<input type="text" class="form-control" id="firstname" name="firstname">
</div>
<div class="form-group">
<label for="phone">Phone</label>
<input type="text" class="form-control" id="phone" name="phone" />
</div>
<div class="form-group">
<label for="captcha">Insert captcha... <span>*</span></label>
<input type="text" class="form-control" id="captcha" name="captcha" />
</div>
.....
</fieldset>
</form>
脚本
var formContact = $("#form-contact");
function sendContactEmail() {
var data = formContact.serialize();
console.log(data); // Output: (empty)
$.ajax({
url: "/wp-admin/admin-ajax.php",
type: "POST",
data: data + '&action=sendcontactmail',
cache: false,
success: function (html) {
...
},
error: function() {
...
}
});
}
/*
* Validation Form with jquery.validate plugin
*/
formContact.validate({
rules:{
...
'captcha':{
required: true,
number: true,
remote: urlCaptcha // If I comment this line all works
}
...
}, //rules
onkeyup: false,
onfocusout: false,
submitHandler: function() {
sendContactEmail();
}
}); // end Validate
取而代之的是:
$("footer").click(function() {
var dataform = formContact.serialize();
console.log(dataform); // Output: OK, all the form values are here
});
修改:
也许我发现了问题所在。它似乎与jquery验证规则相关。我对表单中的验证码输入有一个规则。如果我对行remote: urlCaptcha
的评论都有效,但我显然需要该行检查验证码。
规则
'captcha':{
required: true,
number: true,
remote: urlCaptcha
}
urlCaptcha 在head:
中定义<script type="text/javascript">var urlCaptcha = 'http://localhost/project/wp-content/themes/my_theme/assets/js/plugins/captcha/process.php';</script>