jQuery 如何检查函数的返回值是真还是假

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12425632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:39:41  来源:igfitidea点击:

How to check the function's return value if true or false

jqueryformsif-statement

提问by Ken

I have a function here that validate fields in a form if they are empty.

我在这里有一个函数可以验证表单中的字段是否为空。

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;
}

Now I wanted to use that function here:

现在我想在这里使用该功能:

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");
    });
}

I tried it and I come up with this.

我试过了,我想出了这个。

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");
    });
 }
}

The problem is that, the validation is working.. however, .post()function runs even though there is empty field. I guess its on the if/else condition.. is there a better way to accomplish this?

问题是,验证正在工作......但是,.post()即使有空字段,函数也会运行。我猜它在 if/else 条件下......有没有更好的方法来完成这个?

Thank you.

谢谢你。

回答by Christian

false != 'false'

For good measures, put the result of validate into a variable to avoid double validation and use that in the IF statement. Like this:

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

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

回答by JDandChips

You don't need to call ValidateForm()twice, as you are above. You can just do

你不需要ValidateForm()像上面那样打电话两次。你可以做

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

I think that will solve the issue as above it looks like your comparing true/falseto the string equivalent 'false'.

我认为这将解决上述问题,看起来您与true/false字符串等效'false'.

回答by ferry

you're comparing the result against a string ('false') not the built-in negative constant (false)

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

just use

只是使用

if(ValidateForm() == false) {

or better yet

或者更好

if(!ValidateForm()) {

also why are you calling validateForm twice?

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

回答by lc.

Wrong syntax. You can't compare a Boolean to a string like "false" or "true". In your case, just test it's inverse:

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

if(!ValidateForm()) { ...

You couldtest against the constant false, but it's rather ugly and generally frowned upon:

可以针对常量 false 进行测试,但它相当丑陋且通常不受欢迎:

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

回答by Ricardo Alvaro Lohmann

ValidateFormreturns boolean,not a string.
When you do this if(ValidateForm() == 'false'), is the same of if(false == 'false'), which is not true.

ValidateForm返回boolean,而不是一个string
当您执行此操作时if(ValidateForm() == 'false'), 与 相同if(false == 'false'),这是不正确的。

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