简体   繁体   English

函数返回假甚至条件为真为什么?

[英]Function is returning false even the condition is true why?

function checkform(for1)
{ 
   var result=checkform1(for1);
   alert(result);
}
function checkform1(form)
{
    var valid=false;
    var val1 = document.getElementById('security_code').value;
    $.get(url, function(data) {
        if(data.toString()==val1.toString())
        {
            var elem = document.getElementById("captchaerr");
            elem.style.visibility = 'hidden';
            valid=true;
        }
        else
        {
            var elem = document.getElementById("captchaerr");
            elem.style.visibility = 'visible';
            valid=false;
        }           
    }); 
    return valid;
}

result alert is always false even the condtion(data.toString()==val1.toString()) is true how the control passes in this. 结果警报始终为false,即使条件(data.toString()== val1.toString())为true,控件如何通过它传递。 thnks.. thnks ..

By default $.get aka Ajax.get is asynchronous (it runs in the background). 默认情况下,$。get aka Ajax.get是异步的(它在后台运行)。 So your function "checkform1" is returning before the Ajax request finishes and sets the "valid" variable. 因此,您的函数“ checkform1”将在Ajax请求完成并设置“ valid”变量之前返回。

Don't use toString(), as string. 不要使用toString()作为字符串。 toString() will return string . toString()将返回string Just test data == val1 . 只是测试data == val1

You are making a asynchronous call, so if you want to check for the result then you have to do this inside the callback 您正在进行异步调用,因此如果要检查结果,则必须在回调中执行此操作

 function checkform1(form)
{
    var valid=false;
    var val1 = document.getElementById('security_code').value;
    $.get(url, function(data) {
        if(data.toString()==val1.toString())
        {
        var elem = document.getElementById("captchaerr");
        elem.style.visibility = 'hidden';
        valid=true;
        }
        else
        {
        var elem = document.getElementById("captchaerr");
        elem.style.visibility = 'visible';
        valid=false;
        }      
        //here you will get the valid and 
        //not outside...outside it will be always false.
    }); 

    //This line will be executed immediately after the previous line
    // and will not wait for the call to complete, so this needs to be done 
    // in the callback. 
    return valid;

}

If your 'get' ajax call is an async one, you can use a callback to get the result: 如果您的“ get” ajax调用是异步调用,则可以使用回调来获取结果:

function checkform(for1)
{ 
   checkform1(for1, function(result) //provide the callback to the async function
   {
     alert(result); 
   });      
}
function checkform1(form, callback)
{
    var valid=false;
    var val1 = document.getElementById('security_code').value;
    $.get(url, function(data) 
    {
        if(data.toString()==val1.toString())
        {
            var elem = document.getElementById("captchaerr");
            elem.style.visibility = 'hidden';
            valid=true;
        }
        else
        {
            var elem = document.getElementById("captchaerr");
            elem.style.visibility = 'visible';
            valid=false;
        }           
        if(callback)
            callback(valid);// call the callback INSIDE of the complete callback of the 'get' jquery function
    }); 

}

Just before condition put alert on both values alert(val1 + '---' + data) and see if its exactly same . 在条件之前,将Alert放在两个值alert(val1 +'---'+ data)上,看看其是否完全相同。

Also trim them before comparing just in case it has some padding to it. 在比较之前也要修剪它们,以防万一有一些填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使条件为true,在HTML中使用Javascript也会返回false - Using Javascript in HTML returning false even if the condition is true 为什么是?真:'假'? “真”返回“真”? - Why is !true ? 'false' : 'true' returning 'true'? 为什么这个假条件返回真? - Why this false condition returns true? 为什么这个函数在返回true时返回false? - why is this function returning false when it should return true? 如果带有三元运算符的条件不返回真或假 - if condition with ternary operator not returning true or false 为什么即使不满足条件,用JS编写的Function也会返回true? - Why does this Function written in JS return true even if the condition is not satisfied? await function 返回一个 promise 并且即使条件为假也返回 true | 反应功能组件 - await function returns a promise and returns true even if the condition is false | React Functional Component 为什么这个函数只返回真,因为没有 else 语句使它在非真条件下返回假? - Why does this function return only true, since there is no else statement to make it return false under a non true condition? 为什么过滤器 function 即使不通过条件也会返回数组 - Why filter function is returning array even when it does not pass the condition 三元运算符返回“true:false”。为什么? - Ternary operators returning “true : false”. Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM