简体   繁体   English

当另一个复选框“激发”时,不会触发Checkbox .change()事件

[英]Checkbox .change() event doesn't fire, when “fired” by another checkbox

I'm using a checkbox to check all other checkboxes in a table. 我正在使用一个复选框来检查表中的所有其他复选框。
After a checkbox being checked INSIDE the table (on change) I sum up every textbox value in the same tr. 在表格中选中一个复选框后(更改时),我总结了同一tr中的每个文本框值。

It works well on Firefox and Chrome. 它在Firefox和Chrome上运行良好。

On IE11 and below (it's important for me that it works in EVERY browser) the first change of the "all" checkbox just tick the others, but doesn't sum up the textbox values. 在IE11及以下版本上(对我来说很重要,它适用于每种浏览器),“全部”复选框的第一个更改只是勾选了其他复选框,而不是汇总文本框的值。
On the second click he sums up but untick the checkboxes. 在第二次单击时,他进行了总结,但取消了复选框。
So it works in the wrong way because the first "change" is ignored or sth. 因此它以错误的方式工作,因为第一个“更改”被忽略或其他。

Sorry for my bad english and bad description. 对不起,我的英语不好,描述不好。

<thead>
    <tr>
        <th>Auswahl <input type="checkbox" id="allcheck" /></th>
        <th>Kunden-Nr.</th>
        <th>Personal-Nr.</th>
        <th>Vorname</th>
        <th>Name</th>
        <th>Gutscheintyp</th>
        <th>Betrag €</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td align="center"><input type="checkbox" name="check" /></td>
        <td align="left">1</td>
        <td align="left">2</td>
        <td align="left">3</td>
        <td align="left">4</td>
        <td align="left" class="typ">5</td>
        <td align="center" class="texttd">
        <input type="text" class="txt" maxlength="5" onclick="this.select()" value="20.00" /></td>
    </tr>
</tbody>
    <tfoot>
        <tr>
            <th colspan="5"></th>
            <th colspan="1"">Summe</th>
            <th colspan="1" id="sum">0,00</th>
        </tr>
    </tfoot>


allcheck is the id of the checkbox, checking all others. allcheck是复选框的ID,选中所有其他复选框。 :) :)

// TICK ALL CHECKBOXES
$('#allcheck').click (function () {
    var checkedStatus = this.checked;
    $('table tbody').find('input[type="checkbox"]').each(function () {
        $(this).prop('checked', checkedStatus);
    });
});



// SUM UP TEXTBOXES IF CHECKBOX CHECKED
$('input[type="checkbox"]').change(function() {
    var sum = 0.00;
    $("table tr td input:checked").each(function(){
        test = $(this).closest('tr').find('input[type="text"]').val();
        test = test.replace( ",",".");
    if (isNaN(test)) {
        sum = 0.00;
    }
        sum += parseFloat(test, 10);
    $("#sum").html(sum.toString().replace(".",","));
});
    sum = parseFloat(sum, 10);
    sum = sum.toFixed(2);
    $("#sum").html(sum.toString().replace(".",","));
});

I hope these information are enough for you to help me a little. 希望这些信息对您有所帮助。 :) :)

instead of click event use change event for all check checbox. 代替click事件,对所有复选框都使用change事件。 on change will fire after changing to new value and this.checked will give you correct value : 更改后将触发更改为新值, this.checked将为您提供正确的值:

$('#allcheck').change(function() {
    var checkedStatus = this.checked;
    $('table tbody').find('input[type="checkbox"]').each(function () {
        $(this).prop('checked', checkedStatus);
    });
});

Seems that changing "checked" property does not always trigger "change" event. 似乎更改“选中”属性并不总是触发“更改”事件。

So you must move your sum calculation into separate function and call it explicitly in "#allcheck" click handler 因此,您必须将总和计算移到单独的函数中,并在“ #allcheck”点击处理程序中显式调用它

(also cleaned your code a bit) (也清除了您的代码)

// TICK ALL CHECKBOXES
$('#allcheck').click (function () {
    var checkedStatus = this.checked;
    $('table tbody').find('input[type="checkbox"]').each(function () {
        $(this).prop('checked', checkedStatus);        
    });
    updateSum();
});

// SUM UP TEXTBOXES IF CHECKBOX CHECKED
$('tbody input[type="checkbox"]').change(updateSum);

function updateSum() {
    var sum = 0.00;
    $("tbody input:checked").each(function(){
        var test = $(this).closest('tr').find('input[type="text"]').val();
        var testNum = parseFloat(test.replace( ",", "."));
        if (!isNaN(testNum)) {
            sum += testNum;
        }        
    });    
    $("#sum").text(sum.toFixed(2).replace(".",","));
}

JSFiddle JSFiddle

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

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