jquery change attribute of icheck plugin

有些话、适合烂在心里 提交于 2019-12-08 08:43:29

问题


I have this code to style a checkbox using the iCheck plugin for jquery, is there a way to change the 'insert' attribute later?. I tried recreating the iCheck but the callback function is deleted if I do it that way.

$(this).iCheck({
    checkboxClass: checkboxClass,
    insert: '<div class="icheck_line-icon"></div>' + $(this).attr("data-label")
});

$(this).on({ifChecked: function(){
    iCheckChange(this, yes);
},ifUnchecked: function(){
    iCheckChange(this, no);
}
});

回答1:


Lets say you have checkboxes in this format:

<input type="checkbox" class="my_checkbox" data-label="Check me" />

Than you can change your icon and text this way:

var checkboxClass = 'checkbox_cls';
$('.my_checkbox').each(function(i) {
    var thisCheckbox=$(this);
    thisCheckbox.iCheck({
        checkboxClass: checkboxClass,
        insert: '<div class="icheck_line-icon"></div><span class="insert_label">' + thisCheckbox.data("label") + '</span>'
    });
    var thisIcon = thisCheckbox.siblings('.icheck_line-icon');
    var thisLabel = thisCheckbox.siblings('.insert_label');
    thisCheckbox.on('ifChecked', function(e){
        thisIcon.addClass('icon-checked');
        thisLabel.html('Im checked');
    });
    thisCheckbox.on('ifUnchecked', function(e){
        thisIcon.removeClass('icon-checked');
        thisLabel.html('Not checked');
    });
});

Take a look at this Fiddle.



来源:https://stackoverflow.com/questions/28610485/jquery-change-attribute-of-icheck-plugin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!