如何从jquery函数触发jquery函数

时间:2015-02-24 22:30:53

标签: jquery

我有单独的复选框jquery函数来隐藏/显示列。

$("input:checkbox:not(:checked)").each(function () {
    var column = "table ." + $(this).attr("name");
    $(column).hide();
});

$("input:checkbox").click(function () {
    var column = "table ." + $(this).attr("name");
    $(column).toggle();
});

我还有一个选择/取消选择所有jquery:

$('#select_all').click(function (event) {
if (this.checked) {
    // Iterate each checkbox
    $(':checkbox').each(function () {
        this.checked = true;
        this.trigger("click");//this doesn't work
    });
}
else {
    // Iterate each checkbox
    $(":checkbox").each(function () {
        this.checked = false;
        this.trigger("click");//this doesn't work either
    });
}
});

如何从选择所有点击功能中触发点击功能?

2 个答案:

答案 0 :(得分:0)

您可以创建单独的功能

function hideColumn(obj){
    var column = "table ." + obj.attr("name");
    $(column).hide();
}

function showColumn(obj){
    var column = "table ." + obj.attr("name");
    $(column).toggle();
}

您的代码将是:

$("input:checkbox:not(:checked)").each(function () {
    hideColumn($(this));
});

$("input:checkbox").click(function () {
    showColumn($(this));
});

要(取消)全选:

$('#select_all').click(function (event) {
if (this.checked) {
    // Iterate each checkbox
    $(':checkbox').each(function () {
        this.checked = true;
        hideColumn($(this));
    });
}
else {
    // Iterate each checkbox
    $(":checkbox").each(function () {
        this.checked = false;
        showColumn($(this));
    });
}
});

答案 1 :(得分:0)

this是javascript DOM对象。你需要一个jQuery对象来触发:$(this).trigger('click');