从click函数调用时,函数不评估true / false

时间:2016-01-18 11:15:37

标签: jquery

我试图在单击表单提交按钮时将变量传递给函数:

function validateMyTransfer()
{

    var accounttype = $("#transfer_from option:selected").attr('accountdescription');
    if (accounttype == "Current Account")
    {
        var sharebalance = $("#transfer_from option:selected").attr('balance');

        if (newloanbalance > sharebalance)
        {
            alert("You cannot transfer an amount greater than that which is owed in loans from a Share Account.")
            return false
        }
    }
    return true
}

位于document.ready之前的上述功能会检查所选帐户是否为当前帐户。如果是,则根据所有贷款帐户的总余额检查余额(这是从第二个代码块传入的)。如果贷款余额大于帐户余额我希望返回false,请停止表单提交并提醒用户。

var loanbalance = 0;
$('#transfer_from option').each(function()
{
    var producttypeid = $(this).attr('producttype');
    var balance =  $(this).attr('balance');

    if (balance == 0 || producttypeid == 2)
    {
        loanbalance = parseFloat(loanbalance) + parseFloat(balance);
        $(this).remove();
    }
});

$("#loanbalance").val(loanbalance);
newloanbalance = loanbalance;

$("#transfer-submit").click(validateMyTransfer(newloanbalance));

位于document.ready块中的上述代码贯穿每个选择选项,如果其余额为0或producttype为2(贷款产品),则此选项的余额将添加到loanbalance并且该选项将从选择。然后我将其传递给具有newloanbalance的validateMyTransfer。问题是它没有将函数评估为真或假,仍然提交表单。我该如何纠正这个?

1 个答案:

答案 0 :(得分:0)

您需要解析shareBalance:

var sharebalance = parseInt($("#transfer_from option:selected").attr('balance'));

var sharebalance = parseFloat($("#transfer_from option:selected").attr('balance'));