javascript - Checkbox div double the label? -
i don't know why when checkbox clicked, double label text associated. me?
// add multiple select / deselect functionality $("#selectall").click(function () { $('.child').attr('checked', this.checked); $('.ck,.chkbox,.checkall ,input:radio').dcustominput(); }); jquery.fn.dcustominput = function(){ $(this).each(function(i){ if($(this).is('[type=checkbox],[type=radio]')){ var input = $(this); var id=input.attr('id'); // associated label using input's id var forlabel = $('label[for='+input.attr('id')+']'); var chklabel = forlabel.text(); forlabel.hide(); var label = $("<label for='"+id+"' class='checker'>"+chklabel+"</label>"); //get type, classname suffix var inputtype = (input.is('[type=checkbox]')) ? 'checkbox' : 'radio'; // alert(label); // wrap input + label in div $('<div class="custom-'+ inputtype +'"></div>').insertbefore(input).append(input, label); // find inputs in set using shared name attribute if(input.is(':disabled')){ if(inputtype == 'checkbox' && input.is(':checked')){ label.addclass(' checkeddisabled '); } else{ label.addclass(' disabled '); } } // necessary browsers don't support :hover pseudo class on labels label.hover( function(){ if(!input.is(':disabled') ){ $(this).addclass('hover'); } if(inputtype == 'checkbox' && input.is(':checked') && !input.is(':disabled')){ $(this).addclass('checkedhover'); } }, function(){ $(this).removeclass('hover checkedhover focus'); } ); //bind custom event, trigger it, bind click,focus,blur events input.bind('updatestate', function(){ if (input.is(':checked') && !input.is(':disabled')) { if (input.is(':radio')) { var allinputs = $('input[name='+input.attr('name')+']'); allinputs.each(function(){ $('label[for='+$(this).attr('id')+']').removeclass('checked'); }); }; label.addclass('checked '); } else { label.removeclass('checked checkedhover checkedfocus '); } }) .trigger('updatestate') .click(function(){ $(this).trigger('updatestate'); }) .focus(function(){ label.addclass('focus'); if(inputtype == 'checkbox' && input.is(':checked')){ $(this).addclass('checkedfocus'); } }) .blur(function(){ label.removeclass('focus checkedfocus'); }); } }); }; </script>
i'm not aware of dcustominput guess $('.ck,.chkbox,.checkall ,input:radio').dcustominput(); need placed on document ready , not on click handler
Comments
Post a Comment