简体   繁体   English

带有if else语句的javascript函数忽略返回true/false

[英]javascript function with if else statements ignore return true/false

So I have a PHP form which is being submitted with isset($_POST[]) .所以我有一个用isset($_POST[])提交的 PHP 表单。 I wrote a js function which runs some code with if-else which should return true/false but it's not and the form is getting submitted regardless.我写了一个 js 函数,它运行一些带有if-else代码,它应该return true/false但事实并非如此,并且无论如何都会提交表单。

I've tried using e.preventDefault() but that hasn't worked for me either.我试过使用e.preventDefault()但这对我也不起作用。 I also tried putting the true/false in a var as so out = true/false (Of course I put them in different var as for if and else), and then tried returning them as return out .我还尝试将 true/false 放在 var 中,因为out = true/false (当然,我将它们放在不同的 var 中,因为 if 和 else ),然后尝试将它们作为return out Code for true/false in var - var 中真/假的代码 -

function submitFunc(a, b) {
   if (a >= b) {
        alert("*Duration cannot be greater than Quoted Hours");
        out = false;
    }       
    else {
        out = true;
    }   
    window.location = "add_working_details.php";
    return out;

}

Original Code -原始代码 -

function submitFunc(a, b) {

    if (a >= b) {
        alert("*Duration cannot be greater than Quoted Hours");
        // event.preventDefault();
        //out = false;
        return false;
                window.location = "add_working_details.php";
    }       
    else {
        // out = true;
        return true;
    }   
    //return out;
    // window.location = "add_working_details.php";
}

The AJAX function- AJAX 函数——

$("#submit").click(function(){//comparing the total quoted hours with duration of time selected         
        ticket_id = $('#ticket_no').val();
        total_hours = $('#total_hours').val(); 

        var user_dur, ud, th;

        $.ajax({
            url: 'comp_time.php',
            type: 'GET',
            data: {ticket_id: ticket_id, total_hours: total_hours} ,
            success: function (response) {
                // console.log("response", response);
                var resp = response;
                user_dur = $('#time_duration').text();
                ud = compare_time(user_dur); //user duration
                th = Number(resp); //total quoted hours

                submitFunc(ud, th);
            }

        }); 
    });

So I believe that returning false should stop the form from submitting and then with the window.location it should redirect to the page I want but it's not doing so.所以我相信返回false应该停止提交表单,然后使用window.location它应该重定向到我想要的页面,但它没有这样做。 Please correct me if my logic is wrong.如果我的逻辑错误,请纠正我。 Thanks for your time.谢谢你的时间。

First your function should prevent the submit regardless of the event outcome首先,无论事件结果如何,您的功能都应阻止提交

$("#submit").click(function(e){
e.preventDefault();

second you need to remove the returns from your function, they don't affect you click event其次,您需要从函数中删除返回值,它们不会影响您的点击事件

function submitFunc(a, b) {

    if (a >= b) {
        alert("*Duration cannot be greater than Quoted Hours");       
    } else {
       window.location = "add_working_details.php";
    }
}
$('#form').submit(function(e) {
   if (submitFunc(1, 2)) {
   $.ajax({});
    } else {
        e.preventDefault();
    }
})

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM