如果选中复选框,请执行此操作,这样做不起作用

时间:2016-08-10 09:07:44

标签: jquery checkbox

我正在尝试添加税,如果复选框是单击或不添加它,如果不是也是如果在添加金额添加税后添加税后复选框,反之亦然,如果未选中则删除税。我的代码如下,但我收到updatetotal的错误消息不是函数。从我所读到的,这应该工作,但不是。请忽略$j这是针对数据库的非冲突jquery设置

$j(function() {

        $j(document).on('keyup', '#addit_fee_cost_net', function() {  
            updateTotal();
        });
        var updateTotal = function () {
          addit_fees = parseFloat($j('#addit_fee_cost_net').val());

        var output_total = $j('#addit_fee_cost_gross');

        var total = addit_fees;
            if(isNaN(total)){
            total=0.00;
            }
        output_total.val(total.toFixed(2));
        };          

        $j(document).on('change','#addit_taxable_flag', function() { 
    if($j(this).is(":checked")) {           
            updateTotal();

        var updateTotal = function () {
          addit_fees = parseFloat($j('#addit_fee_cost_net').val());

        var output_total_inc_vat = $j('#addit_fee_cost_gross');

        var tax = parseFloat($j('#tax_id').val());

        var total_inc_vat = addit_fees * (tax + 1);
            if(isNaN(total_inc_vat)){
            total_inc_vat=0.00;
            }
        output_total_inc_vat.val(total_inc_vat.toFixed(2));
        };
    } else {
            updateTotal();
                    var updateTotal = function () {
          addit_fees = parseFloat($j('#addit_fee_cost_net').val());

        var output_total = $j('#addit_fee_cost_gross');

        var total = addit_fees;
            if(isNaN(total)){
            total=0.00;
            }
        output_total.val(total.toFixed(2));
        };  
    }
    });
});

我刚刚添加了以下答案,所以我现在没有得到未经解决的错误,但是如果检查过没有工作,那么没有任何改变。

$ j(function(){

        $j(document).on('keyup', '#addit_fee_cost_net', function() {  
            updateTotal();
        });
        var updateTotal = function () {
          addit_fees = parseFloat($j('#addit_fee_cost_net').val());

        var output_total = $j('#addit_fee_cost_gross');

        var total = addit_fees;
            if(isNaN(total)){
            total=0.00;
            }
        output_total.val(total.toFixed(2));
        };          

        $j(document).on('change','#addit_taxable_flag', function() { 
    if($j(this).is(":checked")) {   

        var updateTotal = function () {

            updateTotal();

          addit_fees = parseFloat($j('#addit_fee_cost_net').val());

        var output_total_inc_vat = $j('#addit_fee_cost_gross');

        var tax = parseFloat($j('#tax_id').val());

        var total_inc_vat = addit_fees * (tax + 1);
            if(isNaN(total_inc_vat)){
            total_inc_vat=0.00;
            }
        output_total_inc_vat.val(total_inc_vat.toFixed(2));
        };
    } else {
        var updateTotal = function () {

            updateTotal();

          addit_fees = parseFloat($j('#addit_fee_cost_net').val());

        var output_total = $j('#addit_fee_cost_gross');

        var total = addit_fees;
            if(isNaN(total)){
            total=0.00;
            }
        output_total.val(total.toFixed(2));
        };  
    }
    });
});

任何帮助都将非常感激,因为无论我如何尝试格式化我无法让它运行我刚刚尝试了以下格式但没有任何火灾。非常感谢提前

    function updateTotal1(checked){
        if(checked == true){

        var updateTotal1 = function () {

            updateTotal1();

          addit_fees = parseFloat($j('#addit_fee_cost_net').val());

        var output_total_inc_vat = $j('#addit_fee_cost_gross');

        var tax = parseFloat($j('#tax_id').val());

        var total_inc_vat = addit_fees * (tax + 1);
            if(isNaN(total_inc_vat)){
            total_inc_vat=0.00;
            }
        output_total_inc_vat.val(total_inc_vat.toFixed(2));
        };
    } else {

        var updateTotal1 = function () {

            updateTotal1();

          addit_fees = parseFloat($j('#addit_fee_cost_net').val());

        var output_total = $j('#addit_fee_cost_gross');

        var total = addit_fees;
            if(isNaN(total)){
            total=0.00;
            }
        output_total.val(total.toFixed(2));
    };
    };
    };
});

    $j('#addit_taxable_flag').on('change', function(){
         updateTotal($j(this).is(':checked'));
    }); 

4 个答案:

答案 0 :(得分:1)

经过一场大规模的斗争和一些外界的帮助,我设法解决了这个问题。非常感谢你的帮助。道歉是这里的代码。

$j(function() {

        $j(document).on('keyup', '#addit_fee_cost_net', function() {  
            updateTotal();
        });
        var updateTotal = function () {
          addit_fees = parseFloat($j('#addit_fee_cost_net').val());

        var output_total = $j('#addit_fee_cost_gross');
        var tax = parseFloat($j('#tax_id').val());

        var total = addit_fees;
            if(isNaN(total)){
            total=0.00;
            }
            if($j('#addit_taxable_flag').is(":checked")) {
        var total = addit_fees * (tax + 1);
            }
        output_total.val(total.toFixed(2));
        };          

        $j(document).on('change','#addit_taxable_flag', function() {
            if($j(this).is(":checked")) {   
                    updateTotal2();
            } else {
                    updateTotal3();
            }
        });

    var updateTotal2 = function () {
        addit_fees = parseFloat($j('#addit_fee_cost_net').val());

        var output_total_inc_vat = $j('#addit_fee_cost_gross');

        var tax = parseFloat($j('#tax_id').val());

        var total_inc_vat = addit_fees * (tax + 1);
        if(isNaN(total_inc_vat)){
            total_inc_vat=0.00;
        }
        output_total_inc_vat.val(total_inc_vat.toFixed(2));
    };
    var updateTotal3 = function () {
        addit_fees = parseFloat($j('#addit_fee_cost_net').val());

        var output_total = $j('#addit_fee_cost_gross');

        var total = addit_fees;
        if(isNaN(total)){
            total=0.00;
        }
        output_total.val(total.toFixed(2));
    };  

});

答案 1 :(得分:0)

updateTotal()在被定义为函数之前被调用。你应该把它放在var updateTotal = function(){...}之后。或者,我只是直接执行你拥有的逻辑。

答案 2 :(得分:0)

在定义变量之前,当函数存储在变量中时,您无法调用该函数!应在控制台中弹出updateTotal is undefined错误。

$j(document).on('keyup', '#addit_fee_cost_net', function() {  
    updateTotal();//undefined
});
var updateTotal = function () {
  addit_fees = parseFloat($j('#addit_fee_cost_net').val());

首先使用函数定义变量,然后调用它。

var x = function () {};
x();

答案 3 :(得分:0)

在$(文档).ready({...})之前定义你的函数,因为你需要在“函数调用者触发器加载”之前定义你的函数

function updateTotal(status){
 if(status == true){
  // if checked
 }
 else {
  // if not checked
 }
}

jQuery的:

$j('#addit_taxable_flag').on('change', function(){
 updateTotal($j(this).is(':checked'));
});