所以我有一個正在提交的 PHP 表單isset($_POST[])。我寫了一個 js 函數,它運行一些if-else應該使用的代碼,return true/false但它不是,并且無論如何都會提交表單。我試過使用,e.preventDefault()但這對我也不起作用。我還嘗試將 true/false 放在 var 中out = true/false(當然,我將它們放在不同的 var 中作為 if 和 else),然后嘗試將它們作為return out. 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;}原始代碼 -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";}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); } }); });所以我相信返回false應該停止提交表單,然后window.location它應該重定向到我想要的頁面,但它沒有這樣做。如果我的邏輯有誤,請糾正我。謝謝你的時間。
帶有if else語句的javascript函數忽略返回true/false
料青山看我應如是
2022-01-02 15:58:28