简体   繁体   English

为什么我的函数返回“未定义”而不是布尔值

[英]Why is my function returning “undefined” instead of boolean

$(document).ready(function() {
    $("form").submit(function(e) {

         if (!checkStockOfProduct())
         {
           e.preventDefault();
           return false;
         }
         return true
    });
});

<script src="~/Scripts/storeinputoutput.js"></script>

storeinputoutput.js file: storeinputoutput.js 文件:

function checkStockOfProduct() {
    var allowSubmitForm = true;
    $("table#StoreItemdatatable > tbody > tr").each(function () {

    .
    .
    .

        var storeId = $('#StoreID').val();

        var stock = null;
        var URL = '/aaa/bbb' 
        $.ajax({
            async: false,
            url: URL,
            contentType: 'application/x-www-form-urlencoded',
            dataType: 'json',
            data: {
                productNServiceID: productNServiceID,
                storeID: storeId
            },
            type: 'POST',
            success: function (data) {
                stock = data.Stock;
            }
            , error: function (jqXHR, exception) {

            }

        });



        if (...) {
            allowSubmitForm = false;
        }
        return allowSubmitForm;

    });
}

In form submit , I called the function and I wouldn't do the submit operation if function return false.在 form submit 中,我调用了该函数,如果函数返回 false,我不会执行提交操作。 But the value of the xx variable is always undefined.但是 xx 变量的值总是未定义的。

Please advise请指教

Checked using Alert.使用警报检查。 variable "allowSubmitForm" in function "checkStockOfProduct" in the last line has value and not "undefined" But in Submit it is undefined最后一行中函数“checkStockOfProduct”中的变量“allowSubmitForm”具有值而不是“undefined”但在Submit中它是undefined

  1. I understand you want to return boolean, so use true and false rather than 'true' and 'false' Why?我知道你想返回布尔值,所以使用 true 和 false 而不是 'true' 和 'false' 为什么? The ones without quotes are boolean while the ones with quotes are string.不带引号的是布尔值,带引号的是字符串。 A non-empty string in javascript would always return true while an empty string would return false (youare returnimg non-empty strings there and the result is obviously not what you would want in this case). javascript 中的非空字符串将始终返回 true 而空字符串将返回 false(您在那里 returnimg 非空字符串,结果显然不是您在这种情况下想要的)。

  2. var is a little tricky and I would suggest we use let next time. var 有点棘手,我建议我们下次使用 let 。 Since it doesnt seem as though you are doing a lot in the checkStockOfProduct function I would refactor it to ==>由于您在 checkStockOfProduct 函数中似乎没有做很多事情,因此我会将其重构为 ==>

const checkSumOfProduct = (whatever...) ? false : true
  1. I noticed the update on your question, I really don't get what you are trying to do there but to me it looks as though the result of the checkStockOfProduct decides whether you would prevent default form submission on submit and return false or not.我注意到您的问题的更新,我真的不明白您在那里尝试做什么,但在我看来,checkStockOfProduct 的结果似乎决定了您是否会在提交时阻止默认表单提交并返回 false。 I would also be assuming that the CheckSumOfProduct function is synchronous Let's refactor that code to do the same thing you were trying to do but look something like this ==>我还假设CheckSumOfProduct函数是同步的让我们重构该代码以执行您尝试执行的相同操作,但看起来像这样 ==>
$(document).ready(function() {
    $("form").submit(function(e) {
        if(!CheckStockOfProduct()){
           e.preventDefault()
           return false
         } 
           return true
    });
});

Let me know if this works out...I am in transit and on mobile atm如果这行得通,请告诉我...我正在运输途中并使用移动自动取款机

Try尝试

function checkStockOfProduct() {
    var allowSubmitForm = true;
    if (...) {

        allowSubmitForm = false;
    }

    return allowSubmitForm;

  };

And adjust your submit() event handler based on the function return value:并根据函数返回值调整您的 submit() 事件处理程序:

$(document).ready(function () {
    $("form").submit(function (e) {
       if (checkStockOfProduct()) {
          //valid
          return;
       }
       //invalid
       e.preventDefault();
    });
});

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

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