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

时间:2019-09-06 05:03:02

标签: javascript php

所以我有一个isset($_POST[])提交的PHP表单。我编写了一个js函数,该函数运行带有if-else的{​​{1}}代码,但事实并非如此,无论如何都将提交表单。

我尝试使用return true/false,但这对我也不起作用。我还尝试将true / false放入e.preventDefault()这样的变量中(当然,我将它们与if和else放在不同的var中一样),然后尝试将其作为out = true/false返回。 var中true / false的代码-

return out

原始代码-

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;

}

AJAX功能-

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

因此,我认为返回$("#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); } }); }); 可以阻止表单提交,然后使用false可以将其重定向到我想要的页面,但它没有这样做。 如果我的逻辑错误,请纠正我。感谢您的宝贵时间。

2 个答案:

答案 0 :(得分:0)

首先,无论事件结果如何,您的函数都应阻止提交

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

您需要从函数中删除返回值,这不会影响您的点击事件

function submitFunc(a, b) {

    if (a >= b) {
        alert("*Duration cannot be greater than Quoted Hours");       
    } else {
       window.location = "add_working_details.php";
    }
}

答案 1 :(得分:-1)

    public override Task<Tag> GetComputeUnitTagsStream(ComputeUnit computeUnit, IServerStreamWriter<Tags> asyncStreamWriter, ServerCallContext ctx)
    {
        return null;
    }


    public override async Task<Empty> SendComputeUnitTagsStream(IAsyncStreamReader<ComputeUnitTags> asyncStreamReader, ServerCallContext ctx)
    {
        while (await asyncStreamReader.MoveNext())
        {
            var current = asyncStreamReader.Current;
        }
        return new Empty();
    }