我有一个来自WordPress的contact-template.php文件...
表单有4个必填字段...
现在,如果任何或所有必填字段为空,表单将不会提交,用户将在每个为空的文本输入字段下面得到“此字段是必填的”消息。-这完全是我想要的
我遇到的问题是,当填写所需的字段,并点击“提交”按钮时,会有10-15秒的停顿,然后显示“谢谢你,我们会尽快回复你”的消息。
由于这种暂停,用户会多次单击submit按钮,并且每次单击都会发送一封重复的电子邮件。
我尝试使用“onclick=”this.disabled=true;this.value='发送,请稍候...“,但这跳过了输入字段验证,不允许发送消息。
我希望此按钮禁用并显示“正在发送,请稍候...”文本,但它需要验证文本输入字段并最终提交/发送电子邮件消息
请救命!!!
这是密码...
<?php /* Template Name: Contact Form */ ?>
<?php get_header(); ?>
<div id="top-div"></div>
<div id="container">
<div id="inner-headline">
<h2>
<?php
$headline = get_post_meta($post->ID, "_headline", $single = false);
if(!empty($headline[0]) )
{
echo $headline[0];
}
else
{
the_title();
}
?>
</h2>
</div>
<div id="content">
<div id="content-inner">
<div class="sideright-left-col">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php if(isset($hasError) || isset($captchaError)):
?>
<p class="error"><?php _e('There was an error
submitting the form.', 'Sona')?><p>
<?php endif ?>
<div id="status"></div>
<form action="" id="contact-form" method="post">
<div class="name">
<label for="contactName"><span style="color:
red;">* </span><?php _e( 'Name', 'Sona' ); ?>:</label>
<input type="text" name="contactName"
id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>"
class="requiredField txt"/>
<?php if(isset($nameError) && $nameError != ''): ?>
<span class="error"><?php echo $nameError;?></span><?php endif;?>
<div class="clear"></div>
</div>
<div class="email">
<label for="email"><span style="color:
red;">* </span><?php _e( 'E-mail', 'Sona' ); ?>:</label>
<input type="text" name="email" id="email" value="<?php
if(isset($_POST['email'])) echo $_POST['email'];?>" class="requiredField email txt" />
<?php if(isset($emailError) &&
$emailError != ''): ?><span class="error"><?php echo $emailError;?></span><?php endif;?>
<div class="clear"></div>
</div>
<div class="subject">
<label for="subject"><span style="color:
red;">* </span><?php _e( 'Subject', 'Sona' ); ?>:</label>
<input type="text" name="subject" id="subject" value="<?
php if(isset($_POST['subject'])) echo $_POST['subject'];?>" class="requiredField txt"/>
<?php if(isset($subjectError) && $subjectError != ''): ?
><span class="error"><?php echo $subjectError;?></span><?php endif;?>
<div class="clear"></div>
</div>
<div class="clear"></div>
<div class="message">
<label for="message"><span style="color:
red;">* </span><?php _e( 'Message', 'Sona' ); ?>:</label>
<textarea name="message" cols="100" rows="200"
id="message" class="txt requiredField"><?php echo isset($_POST['message']) &&
$_POST['message']!=''
? stripslashes($_POST['message']) : ''?></textarea>
<?php if(isset($messageError) &&
$messageError != '') { ?><span class="error"><?php echo $messageError;?></span> <?php } ?>
<div class="clear"></div>
</div>
<div>
<?php
$al_options =
get_option('al_general_settings');
$options = array(
$al_options['al_contact_error_message'],
$al_options['al_contact_success_message'],
$al_options['al_subject'],
$al_options['al_email_address'],
);
?>
<input type="hidden" name = "options" value="<?php
echo implode(',', $options) ?>" />
<input type="hidden" name="siteurl" value="<?php
echo get_option('blogname')?>" />
<br />
<input type="submit" class="button white-back"
value="Submit Message" tabindex="5" id="submit" name="send"/>
<div class="clear"></div>
</div>
</form>
</div>
<div class="sideright-right-col">
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar("Contact
Sidebar") ) : ?> <?php endif;?>
</div>
<div class="clear"></div>
</div>
</div>
</div>
<!-- END CONTENT -->
</div>
<?php endwhile; ?>
<?php endif; ?>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#contact-form").validate({
submitHandler: function() {
var postvalues = jQuery("#contact-form").serialize();
jQuery.ajax
({
type: "POST",
url: "<?php echo get_template_directory_uri() ?>/contact-form.php",
data: postvalues,
success: function(response)
{
jQuery("#status").addClass('success-
message').html(response).show('normal');
jQuery('#contact-form :input.not("#send")').val("");
jQuery('#contact-form :textarea').val("");
}
});
return false;
},
focusInvalid: true,
focusCleanup: false,
rules:
{
contactName: {required: true},
email: {required: true, minlength: 6,maxlength: 50, email:true},
message: {required: true},
subject: {required: true}
},
messages:
{
contactName: {required: "<?php _e( 'This field is required', 'Sona' ); ?>"},
email: {required: "<?php _e( 'This field is required', 'Sona' ); ?>", email: "<?php
_e( 'Please provide a valid e-mail address.', 'Sona' ); ?>"},
message: {required: "<?php _e( 'This field is required', 'Sona' ); ?>"},
subject: {required: "<?php _e( 'This field is required', 'Sona' ); ?>"}
},
errorPlacement: function(error, element)
{
error.insertAfter(element);
},
invalidHandler: function()
{
jQuery("body").animate({ scrollTop: 0 }, "slow");
}
});
});
</script>
<?php get_footer(); ?>
圣洁的耶稣母亲,请使用一个模板引擎,这只是恐怖的阅读。>.<
不管怎样,如果我没有在代码上犯错,您可以用jQuery这样禁用它...
$(document).ready(function(){
$("input[type=submit]").click(function() {
$("input[type=submit]").attr("disabled", "disabled");
)};
}