如果为true或false,如何检查函数的返回值

时间:2012-09-14 13:31:41

标签: 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).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条件下..有没有更好的方法来实现这个目标?

谢谢。

5 个答案:

答案 0 :(得分:17)

false != 'false'

对于好的度量,将validate的结果放入变量中以避免双重验证并在IF语句中使用它。像这样:

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

答案 1 :(得分:10)

您不需要像上面那样两次致电ValidateForm()。你可以做到

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

我认为这样可以解决上述问题,就像您将true/false与等效字符'false'进行比较一样。

答案 2 :(得分:3)

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

只需使用

if(ValidateForm() == false) {

或更好

if(!ValidateForm()) {

为什么你要两次调用validateForm?

答案 3 :(得分:1)

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

if(!ValidateForm()) { ...

可以测试常量错误,但它相当丑陋并且通常不赞成:

if(ValidateForm() == false) { ...

答案 4 :(得分:0)

ValidateForm返回boolean,而不是string 执行此操作时if(ValidateForm() == 'false')if(false == 'false')相同,但这不是真的。

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