我为我的侧边栏创建了一个联系人表单,它在Chrome中工作得很好,但在IE或FF中却没有。
我已经盯着它看了好几个小时了,没发现我做错了什么。
我在任何地方都没有收到任何错误,但当我在FF或IE中提交它时,它会在控制台中记录“nope”,并在#jvmsg中回显一条-1的消息。
我的html是:
<li id="jvcontactwidget-3" class="widget widget_contact">
<div class="title"><h2>Test</h2></div>
<form id="jvcontact" name="widget_contact" action="" method="post">
<label class="required" for="email">Email Address (required)</label>
<input class="required" type="email" name="email">
<label class="required" for="firstName">First Name (required)</label>
<input class="required" type="text" name="firstName">
<label class="required" for="lastName">Last Name (required)</label>
<input class="required" type="text" name="lastName">
<label for="question">Any questions for us?</label>
<textarea rows="5" name="question"></textarea>
<input type="submit" value="Send it" style="display: inline-block; ">
</form>
<div class="jvloading" style="display: none; "></div>
<div id="jvsuccess" style="display: none;" >Thanks for the email</div>
<div id="jvmsg" style="display: none; "></div>
</li>
我的js文件如下:
jQuery(document).ready(function($) {
jQuery(\'#jvcontact\').submit(function() {
jQuery(\'.widget_contact input[type="submit"]\').hide();
jQuery(\'.jvloading\').show();
var str = jQuery(this).serialize();
jQuery.ajax({
type: \'POST\',
url: \'/wp-admin/admin-ajax.php\',
data: \'action=contact_form&\'+str,
success: function(msg) {
//jQuery(\'#jvmsg\').ajaxComplete(function(event, request, settings){
if(msg === \'sent\') {
console.log(\'sent\');
jQuery(\'#jvmsg\').hide();
jQuery(\'.jvloading\').slideUp();
jQuery(\'.widget_contact input[type="submit"]\').slideUp();
jQuery(\'#jvcontact\').slideUp();
jQuery(\'#jvsuccess\').fadeIn(\'slow\');
}
else {
console.log(\'nope\');
jQuery(\'#jvmsg\').html(msg);
jQuery(\'.jvloading\').hide();
jQuery(\'.widget_contact input[type="submit"]\').show();
jQuery(\'#jvmsg\').fadeIn(\'slow\');
}
//});
}
});
return false;
});
});
我的php文件:
function ajax_contact() {
if(!empty($_POST)) {
$error = "";
//Check to make sure sure that a valid email address is submitted
if(trim($_POST[\'email\']) == \'\') {
$error .= "An email address is required<br/>";
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\\.[A-Z]{2,4}$", trim($_POST[\'email\']))) {
$error .= "A valid email address is required<br/>";
} else {
$email = trim($_POST[\'email\']);
}
//Check to make sure that the first name field is not empty
if(trim($_POST[\'firstName\']) == \'\') {
$error .= "Your first name is required<br/>";
} else {
$firstName = trim($_POST[\'firstName\']);
}
//Check to make sure that the last name field is not empty
if(trim($_POST[\'lastName\']) == \'\') {
$error .= "Your last name is required<br/>";
} else {
$lastName = trim($_POST[\'lastName\']);
}
if(trim($_POST[\'question\']) == \'\') {
$question = "They had no questions at this time";
} else {
$question = $_POST[\'question\'];
}
$admin_mail = get_bloginfo(\'admin_email\');
if(empty($error)) {
$message = "Someone wants to get in touch with you. \\n\\n
Name: " . $firstName . " " . $lastName . "\\n
Email: " . $email . "\\n
Question: " . $question . "\\n";
$jvMail = wp_mail($admin_mail, \'Contact from your website\', $message);
echo "sent";
} else {
echo $error;
}
die();
}
}
add_action(\'wp_ajax_contact_form\', \'ajax_contact\');
谢谢你的帮助!