Jquery 如何检查功能';如果为true或false,则返回值

Jquery 如何检查功能';如果为true或false,则返回值,jquery,forms,if-statement,Jquery,Forms,If Statement,这里有一个函数,用于验证表单中的字段是否为空 function ValidateForm() { jQuery('span.error_msg').hide(); var success = true; jQuery("#shippingF input").each(function() { if(jQuery(this).val()=="") { jQuery(this).ne

这里有一个函数,用于验证表单中的字段是否为空

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");
    });
}
函数post(url,formId){
jQuery(#process”).html('alt=“load”title=“ajax loader”width=“16”height=“16”class=“alignnone size full wp-image-134”>);
post(url,jQuery('#'+formId).serialize(),函数(d){
jQuery('html,body').animate({scrollTop:jQuery(#scrollhere”).offset().top},'slow');
jQuery(#response”).html(“

加载可用的支付逃逸….
”width=“220”height=“19”/>”; jQuery(“#response”).load(“,function(){Cufon.refresh();}); jQuery(#response”).attr(“风格”,“高度: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(url,formId){
ValidateForm();
如果(ValidateForm()==“false”){
jQuery('html,body').animate({scrollTop:jQuery('shippingF').offset().top},'slow');
}否则{
jQuery(#process”).html('alt=“load”title=“ajax loader”width=“16”height=“16”class=“alignnone size full wp-image-134”>);
post(url,jQuery('#'+formId).serialize(),函数(d){
jQuery('html,body').animate({scrollTop:jQuery(#scrollhere”).offset().top},'slow');
jQuery(#response”).html(“

加载可用的支付逃逸….
”width=“220”height=“19”/>”; jQuery(“#response”).load(“,function(){Cufon.refresh();}); jQuery(#response”).attr(“风格”,“高度:1030px”); }); } }

问题是,验证正在工作。。但是,
.post()
函数即使有空字段也会运行。我想这是以“如果/否则”为条件的。。有没有更好的方法来实现这一点


谢谢。

您将结果与字符串('false')进行比较,而不是与内置的负常量(false)进行比较

只用

if(ValidateForm() == false) {
或者更好

if(!ValidateForm()) {

还有,为什么要调用validateForm两次?

validateForm
返回
布尔值
,而不是
字符串

执行此操作时,
if(ValidateForm()=='false')
if(false=='false')
相同,但不是真的

function post(url, formId) {
    if(!ValidateForm()) {
        // False
    } else {
        // True
    }
}

语法错误。不能将布尔值与“false”或“true”之类的字符串进行比较。在您的情况下,只需测试它的反向:

if(!ValidateForm()) { ...
你可以针对不断出现的错误进行测试,但它相当丑陋,而且通常不受欢迎:

if(ValidateForm() == false) { ...
为了获得好的度量值,请将validate的结果放入一个变量中以避免双重验证,并在IF语句中使用它。像这样:

var result = ValidateForm();
if(result == false) {
...
}

您不需要像上面那样调用
ValidateForm()
两次。你可以这么做

if(!ValidateForm()){
..
} else ...

我认为这将解决上述问题。您将
true/false
与字符串等价物
'false'

进行比较,是否有理由调用ValidateForm两次?问题是,如果我不包含
ValidateForm()post()函数上执行code>。。错误消息未显示..已在if中调用它。在此之前,您不必再次调用它post()函数上执行code>。。错误消息未显示..在您的示例中,if语句将始终为false,因为您的函数返回true或false,它将永远不会返回'false'。从代码中删除与“false”相关的单引号,它就会起作用。然而,我提出的建议将在您修复代码后改进您的代码地址。在可能未定义的内容上使用.length是危险的。当它实际上是未定义的时,它将返回错误“cannotcalllength on undefined”或类似的内容。很难看出,在这种特殊情况下,它会导致页面刷新post()函数上执行code>。。错误消息不显示。。
if(!ValidateForm()){
..
} else ...