输入不会听取if语句

时间:2015-11-26 13:35:55

标签: javascript jquery html

我有一个输入,在另一个输入keyup上生成一个数字。但是当我的复选框被选中时,它应该只生成这个数字。如果它不是它不应该生成任何东西。但是如果复选框是否检查它会生成我的数字..即使该功能仅存在于已检查模式中..我不知道出了什么问题:

复选框

<div class="col-md-6">
<div class="switch switch-primary">
<input class="multi-transaction" type="checkbox" name="switch" data-plugin-ios-switch="">
</div>
</div>

的jQuery

$('.multi-transaction').click(function () {
    var sum = $('.amount-czk');
    if($(this).prop("checked") == true){
        sum.prop("disabled", false); // check
        sum.css('border-color', '#ccc');
        sum.val(0);

        $('.amount-czk').on('keyup', function () {
            var amount = parseInt($('.give-me-money').val()); // amount to pay in eur example 520
            var amount_wr = parseInt($('.amount-czk').val()); // type how much we got in any currency
            $('.currency-czk').val((amount_wr / amount ? amount_wr / amount : 0).toFixed(2));
        });
        //curchange(true);
    }

    else if($(this).prop("checked") == false){
        console.log('do nothing');
    }
});

输入keyup函数保持工作状态,而不仅仅是给我console.log。

2 个答案:

答案 0 :(得分:3)

不要将事件处理程序绑定到另一个处理程序中,它们会很快繁殖,从而导致奇怪的问题。尝试这样的事情:

$('.multi-transaction').click(function () {
    var sum = $('.amount-czk');
    if ($(this).prop("checked") == true) {
        sum.prop("disabled", false); // check
        sum.css('border-color', '#ccc');
        sum.val(0);
        //curchange(true);
    } else if ($(this).prop("checked") == false) {
        console.log('do nothing');
    }
});

$('.amount-czk').on('keyup', function () {
    if ($('.multi-transaction').prop('checked')) {
        var amount = parseInt($('.give-me-money').val()); // amount to pay in eur example 520
        var amount_wr = parseInt($('.amount-czk').val()); // type how much we got in any currency
        $('.currency-czk').val((amount_wr / amount ? amount_wr / amount : 0).toFixed(2));   
    }
});

答案 1 :(得分:1)

如果条件允许的话......

if($(this).prop('checked')) {
    // something when checked
} else {
    // something else when not
}