Javascript 使用jQuery获取复选框

Javascript 使用jQuery获取复选框,javascript,jquery,checkbox,Javascript,Jquery,Checkbox,我有一个函数,用来捕获对所有复选框的更改。它在手动单击框时工作,但在toggleAll()函数触发框时不工作 要纠正这个问题吗 $(function() { $(":checkbox").change(function(){ if ( this.checked ) { alert(this.id+' is checked'); } else { alert(this.id+' is not checked'); } });

我有一个函数,用来捕获对所有复选框的更改。它在手动单击框时工作,但在
toggleAll()
函数触发框时不工作

要纠正这个问题吗

$(function() {
    $(":checkbox").change(function(){
    if ( this.checked ) {
        alert(this.id+' is checked');
    } else {
        alert(this.id+' is not checked');
    }
    });
});

function toggleAll() {
    var selectAllCheckbox = $("#selectAllCheckbox");
    if ( selectAllCheckbox.prop('checked') ) {
        // uncheck them all
        $("[id^=friendRequestCheckbox]").attr('checked', false);
    } else {
        // check them all
        $("[id^=friendRequestCheckbox]").attr('checked', true);
    }
}

我正在运行一个旧版本的jQuery,还没有更新到新版本,包括
prop
,以防万一。

以编程方式更改元素时,还必须以编程方式触发事件。尝试:

$("[id^=friendRequestCheckbox]").trigger('change')

谢谢,非常简单优雅的解决方案。只要它允许我,我就会把答案记下来。谢谢你的及时答复。