我在这里有一个函数可以验证表单中的字段是否为空。
function ValidateForm()
{
jQuery('span.error_msg').hide();
var success = true;
jQuery("#shippingF input").each(function()
{
if(jQuery(this).val()=="")
{
jQuery(this).next().show();
success = false;
}
});
return success;
}
现在我想在这里使用该功能:
function post(url,formId) {
jQuery("#process").html('<img src="<?php echo get_bloginfo('wpurl').'/wp-content/plugins/'.basename(dirname(__FILE__)).'/images/co/ajax-loader.gif'; ?>" alt="loading" title="ajax-loader" width="16" height="16" class="alignnone size-full wp-image-134">');
jQuery.post(url, jQuery('#' + formId).serialize(), function(d) {
jQuery('html,body').animate({scrollTop: jQuery("#scrollhere").offset().top},'slow');
jQuery("#response").html('<center><p style="height:820px"><span style="color:black;font-weight:bold;font: 11px arial,verdana,sans-serif;"><b>Loading available payment getaways..</b></span><br/><img src="<?php echo get_bloginfo('wpurl').'/wp-content/plugins/'.basename(dirname(__FILE__)).'/images/co/8-1.gif'; ?>" width="220" height="19" /></p></center>');
jQuery("#response").load("<?php echo get_bloginfo('wpurl').'/wp-content/plugins/'.basename(dirname(__FILE__)).'/checkout_payment.php'; ?>", function() { Cufon.refresh(); });
jQuery("#response").attr("style","height:1030px");
});
}
我试过了,我想出了这个。
function post(url,formId) {
ValidateForm();
if(ValidateForm() == 'false') {
jQuery('html,body').animate({scrollTop: jQuery("#shippingF").offset().top},'slow');
} else {
jQuery("#process").html('<img src="<?php echo get_bloginfo('wpurl').'/wp-content/plugins/'.basename(dirname(__FILE__)).'/images/co/ajax-loader.gif'; ?>" alt="loading" title="ajax-loader" width="16" height="16" class="alignnone size-full wp-image-134">');
jQuery.post(url, jQuery('#' + formId).serialize(), function(d) {
jQuery('html,body').animate({scrollTop: jQuery("#scrollhere").offset().top},'slow');
jQuery("#response").html('<center><p style="height:820px"><span style="color:black;font-weight:bold;font: 11px arial,verdana,sans-serif;"><b>Loading available payment getaways..</b></span><br/><img src="<?php echo get_bloginfo('wpurl').'/wp-content/plugins/'.basename(dirname(__FILE__)).'/images/co/8-1.gif'; ?>" width="220" height="19" /></p></center>');
jQuery("#response").load("<?php echo get_bloginfo('wpurl').'/wp-content/plugins/'.basename(dirname(__FILE__)).'/checkout_payment.php'; ?>", function() { Cufon.refresh(); });
jQuery("#response").attr("style","height:1030px");
});
}
}
问题是,验证正在工作。但是,.post()
即使有空字段,函数也会运行。我猜它在 if/else 条件下..有没有更好的方法来完成这个?
谢谢你。