为什么我无法在 if 表达式中调用 Jquery 函数

发布于 2024-10-08 23:29:25 字数 310 浏览 2 评论 0原文

$("#btn1").click(function(){

//CheckMyWeight is called Perfectly here and not in the if loop expression

if ($("#frm1").validationEngine({returnIsValid:true}) && CheckMyWeight() && a === b) {  

}});

为什么我无法在 if 循环表达式中调用“CheckMyWeight()”,但我能够完美调用 在“#btn1”函数之后。

$("#btn1").click(function(){

//CheckMyWeight is called Perfectly here and not in the if loop expression

if ($("#frm1").validationEngine({returnIsValid:true}) && CheckMyWeight() && a === b) {  

}});

why i am not able to call the "CheckMyWeight()" in the if loop expression and i am able to call perfectly
after the "#btn1" function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筱果果 2024-10-15 23:29:25

如果 && 的左侧返回 false,则不会评估右侧。 (因为整个表达式不可能为真)
这称为短路。

如果您想始终调用这两个函数,则应该在条件之外调用它们并将结果分配给变量,并留下注释解释原因

var isValid = $("#frm1").validationEngine({returnIsValid:true});
var isHeavy = CheckMyWeight();   //Always call both functions
if (isValid && isHeavy && a === b) {  
    ...
}

还有其他解决方法,但它们隐藏了代码的意图。

If the left side of the && returns falsy, the right side won't be evaluated. (since the whole expression cannot possibly be true)
This is called short-circuiting.

If you want to always call both functions, you should call them outside the condition and assign the results to variables, and leave a comment explaining why:

var isValid = $("#frm1").validationEngine({returnIsValid:true});
var isHeavy = CheckMyWeight();   //Always call both functions
if (isValid && isHeavy && a === b) {  
    ...
}

There are other workarounds, but they hide the intention of the code.

残花月 2024-10-15 23:29:25

下面是一个小程序,用于测试您是否可以在 if 循环表达式中调用“CheckMyWeight()”。它的工作原理是,问题在于 $("#frm1").validationEngine({returnIsValid:true} 尝试通过打破条件。

 <html>
    <head>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
        alert('body');
        function CheckMyWeight() {
          return true;
        }
        $("#btn1").click(function(){
            var a =1,b=1;
            //CheckMyWeight is called Perfectly here and not in the if loop expression
    //        CheckMyWeight() &&
            if ( CheckMyWeight() && a === b) {
                alert('hi');
        }
        });
    });
    </script>
    </head>
    <body>
    <button id="btn1">Click me</button>
    </body>
    </html>

The below is a small program to test whether u can call the "CheckMyWeight()" in the if loop expression .Its working so the problem is with $("#frm1").validationEngine({returnIsValid:true} try to work by breaking the conditions.

 <html>
    <head>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
        alert('body');
        function CheckMyWeight() {
          return true;
        }
        $("#btn1").click(function(){
            var a =1,b=1;
            //CheckMyWeight is called Perfectly here and not in the if loop expression
    //        CheckMyWeight() &&
            if ( CheckMyWeight() && a === b) {
                alert('hi');
        }
        });
    });
    </script>
    </head>
    <body>
    <button id="btn1">Click me</button>
    </body>
    </html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文