jQuery Uniform Checkbox does not (un)check

后端 未结 8 1934
借酒劲吻你
借酒劲吻你 2020-12-14 00:15

I am using jQuery Uniform to style inputs/selects etcs. However, the checkbox has stopped working. I am appending data sent from an ajax call. Once it\'s loaded

相关标签:
8条回答
  • 2020-12-14 00:50

    I believe the latest version of Jquery uses a different method to evaluate a :checked value. Using a slightly older version (although not ideal) fixed this problem for me. That was a while ago though, and PixelMatrix still don't seem to have updated Uniform.

    0 讨论(0)
  • 2020-12-14 01:01

    I know this may be a bit late, but hopefully it will help someone. I had to add a couple lines to the uniform code so it would look for pre checked checkboxes. This code starts at line 262 for me. I am using Uniform version 1.7.5. The lines between the 'added by John' notes are what I added.

            "click.uniform touchend.uniform": function(){
          if(!$(elem).attr("checked")){
            //box was just unchecked, uncheck span
            spanTag.removeClass(options.checkedClass);
          // Added by John to look for checkboxes with the attr "checked" that have been click to uncheck
          }else if(!$(elem).is(':checked')){
            spanTag.removeClass(options.checkedClass);
          // end addition by john
          }else{
            //box was just checked, check span.
            spanTag.addClass(options.checkedClass);
          }
        },
    
    0 讨论(0)
  • 2020-12-14 01:01

    jQuery 1.6.4 changed the meaning of the attr() function, which is used by jquery.uniform.js. attr() now returns the state that the attribute WAS in when the page loaded - not what the state of the attribute is NOW. There is a replacement function called prop(), which does the job that attr() used to do.

    To fix the plugin, replace each occurrence of attr("checked") with prop("checked"). Also, change attr("disabled") to prop("disabled").

    That should fix your project up. It worked for me ;-)

    See also: https://github.com/pixelmatrix/uniform/pull/167, which attempts to address the issue with better backwards compatibility.

    0 讨论(0)
  • 2020-12-14 01:03

    SIMPLE SOLUTION

    This is because UniformJs requires that you update your modification on uniform elements if you need to change values on the form dynamically:

    $('#checkbox').prop('checked',true);
    $.uniform.update();
    

    Edit

    If you wish to update the #checkbox only:

    $('#checkbox').prop('checked',true);
    $.uniform.update('#checkbox');
    
    0 讨论(0)
  • 2020-12-14 01:12

    Just do this:

    $('#checkbox').prop('checked',true).uniform('refresh');
    
    0 讨论(0)
  • 2020-12-14 01:14

    Its been a while but I had the same problem but I resolved it by a simple solution.

    Uniform is adding a span over the checkbox and gives it the class checked if it is checked or not, but on page load only. If you check or uncheck the checkbox while the page is already loaded, it will uncheck (or check) but since the span is over it, you will see it as not updated, you have to do it manually.

    $('#myDiv input[type=checkbox]').attr('checked',true);
    $('#myDiv input[type=checkbox]').parent().addClass('checked');
    

    or

    $('#myDiv input[type=checkbox]').attr('checked',false);
    $('#myDiv input[type=checkbox]').parent().removeClass('checked');
    

    Might not be the cleanest solution, but works fine on every browser.

    0 讨论(0)
提交回复
热议问题