简体   繁体   English

jQuery单击事件与绑定事件

[英]Jquery click event with binded event

Hi guys I am having a problem with Events. 嗨,大家好,我对活动有疑问。 I have a checkbox list and I have a main check box that checks all boxes. 我有一个复选框列表,并且有一个主复选框可以选中所有复选框。 When I clickEvent some of my checkbox list items it should add data-id attr to the "selected obj". 当我单击事件时,我的某些复选框列表项应将data-id attr添加到“选定的obj”。 So in my case when I press main check box to check all others every thing is ok (it simply clicks all other elements). 因此,在我的情况下,当我按下主复选框以检查所有其他内容时,一切正常(只需单击所有其他元素)。 but when i do that it empties my array. 但是当我这样做时,它会清空我的数组。 I mean if i uncheck it will be the way it supposed to be but checked (when uncheck it fills when i check it empties). 我的意思是,如果我取消选中它将是应该被选中的方式(当我选中它为空时将其取消填充的时候)。

......    
var selected = {};
        var reload = function(){
            selected = {};
            $('.checkbox_all').unbind('click');
            $('.table_checkbox').unbind('click');
            $('.checkbox_all').bind('click', checkAll);
            $('.table_checkbox').bind('click', checkMe);
        }
        var checkMe = function(e){
            var checkbox = $(e.target);
            var id = checkbox.attr('data-id');
            //console.log(id);
            if(checkbox.attr('checked')){
                selected[id] = id;
            }else{
                if(selected[id]) delete selected[id];
            }
            console.log(selected);
        }
        var checkAll = function(e){
            if($(e.target).attr('checked')){
                $('.table_checkbox').each(function(){
                    if($(this).attr('checked') === false){
                        $(this).click();
                    }
                });
            }else{
                $('.table_checkbox').each(function(){
                    if($(this).attr('checked') === true){
                        $(this).click();
                    }
                });
            }
            //console.log(selected);
        }
.......

HTML: HTML:

       <tr><th class="table-header-check"><input type="checkbox" class="checkbox_all"/></th></tr>
    <tr class=""><td><input type="checkbox" data-id="5" class="table_checkbox"></td></tr>
    <tr class="alternate-row"><td><input type="checkbox" data-id="6" class="table_checkbox"</td></tr>
    <tr class="alternate-row"><td><input type="checkbox" data-id="8" 

....ETC\

My problem is that when i click .checkbox_all it should click on all .table_checkbox(that r cheched or uncheched)... it just clicks all checkboxes like a main checkbox... it works fine, but i have an event all other checkboxes if i click em i add some data to array when i unclick em it removes data from array.... so when im clicking checkboxes sepperatly they add /remove data to array properly... but when im clicking on main checkbox... it clicks on right checkboxes but the data array is empty when all checked and full when all unchecked... it must be the opposite way 我的问题是,当我单击.checkbox_all时,应该单击所有.table_checkbox(r已支配或未支配)...它只是像主复选框一样单击所有复选框...可以正常工作,但我有一个事件所有其他复选框如果我单击em,则当我取消单击em时,它将一些数据添加到数组中。...因此,当我分别单击复选框时,它们会正确地添加/删除数据到数组中...但是,当我单击主复选框时,...它单击正确的复选框,但是当所有选中时数据数组为空,而所有未选中时数据数组为满...它必须是相反的

Could you instead go for a cleaner solution, and generate selected on the fly? 您是否可以寻求更清洁的解决方案,并即时生成所选内容? See here for an example (and a JSFiddle for everyone else): http://jsfiddle.net/turiyag/3AZ9C/ 请参见此处的示例(以及其他所有人的JSFiddle): http : //jsfiddle.net/turiyag/3AZ9C/

function selected() {
    var ret = {};
    $.each($(".table_checkbox"),function(index,checkbox) {
        if($(checkbox).prop("checked")) {
            ret[$(checkbox).prop("id")] = true;
        }
    });
    return ret;
}

** EDIT: ** **编辑:**

If you're looking to have an array that is added to and removed from, then this JSFiddle ( http://jsfiddle.net/turiyag/pubGb/ ) will do the trick. 如果您希望添加和删除数组,则此JSFiddle( http://jsfiddle.net/turiyag/pubGb/ )可以解决问题。 Note that I use prop() instead of attr(), in most cases, especially this one, you should use prop() to get the value you want. 请注意,在大多数情况下,尤其是在这种情况下,我使用prop()而不是attr(),应使用prop()获得所需的值。

To work with your own code you need to understand the order of events. 要使用自己的代码,您需要了解事件的顺序。 When you programmatically call click() on the checkbox the javascript (checkMe() for children) executes before the state of each child checkbox is changed (eg, adding attribute 'checked'). 当您以编程方式调用复选框上的click()时,将在更改每个子复选框的状态(例如,添加属性“已选中”)之前执行javascript(子代的checkMe())。 It is because of this reason that the checkMe() function was adding and removing ids in the selected array in the reverse order. 正是由于这个原因,checkMe()函数以相反的顺序添加和删除了所选数组中的ID。 You can confirm this by adding the following debug line in the checkMe function: 您可以通过在checkMe函数中添加以下调试行来确认这一点:

console.log('Checked state of checkbox id:' + id + ' is: ' + checkbox.prop('checked'));

Case1: Clicking checkAll when it is Unchecked; 情况1:未选中时单击checkAll; it calls checkMe() for each child checkbox but finds the 'checked' attribute as undefined. 它为每个子复选框都调用checkMe(),但发现“ checked”属性为未定义。 So it executes the delete code. 因此它执行删除代码。 After executing checkMe the 'checked' attribute is added on the checkbox. 执行checkMe之后,将“ checked”属性添加到复选框。

Case2: Clicking checkAll when it is Checked; 情况2:选中后,单击checkAll; the checkMe() function finds the 'checked' attribute previously added and fills the array. checkMe()函数查找先前添加的“ checked”属性并填充数组。 Later an event is probably fired to remove the 'checked' attribute. 稍后可能会触发一个事件以删除“选中”属性。

I changed the following lines to quickly test this and seems to be working: 我更改了以下几行以快速进行测试,并且似乎可以正常工作:

Bind checkMe on change event instead of click in reload function: 在更改事件上绑定checkMe,而不是单击reload功能:

$('.table_checkbox').bind('change', checkMe);

Change the condition for unchecked children in checkAll function when the .checkbox_all is checked: 选中.checkbox_all时,更改checkAll函数中未选中的子级的条件:

if($(this).prop('checked') === false) {/*call child click*/}
//Use prop instead of attr because it takes care of 'undefined' cases as well. If you want to keep using attr because you're on an older version of jquery then add something like:
typeof $(this).attr('checked') == 'undefined'

and also the condition when .checkbox_all is unchecked: 以及未选中.checkbox_all的条件:

if($(this).prop('checked') === true) {/*call child click*/}

Hope this helps. 希望这可以帮助。 Here's a jsbin to play with.. 这是一个可玩的jsbin

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

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