jQuery Uniform Checkbox does not (un)check

2019-01-22 06:07发布

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, I use $.uniform.update("input:checkbox") to update the new html. When attempting to (un)check the input it works only once. If I want to (un)check it again, it doesn't change at all.

I've attempted changing the Uniform Javascript so that all the actions (ie. click, focus, blur etc) are under the .live function. (ie. .live("click",). This just stops anything from working.

Update:

I've read through the Uniform JS entirely and discovered a problem:

if(!$(elem).attr("checked")){
    //box was just unchecked, uncheck span
    spanTag.removeClass(options.checkedClass);
}else{
    //box was just checked, check span.
    spanTag.addClass(options.checkedClass);
}

The .attr("checked") syntax fails. It will always return false. Why? I don't know. As well as this, it doesn't update the original input to either remove the checked attribute or to set the checked attribute.

I've run through the official website and found it doesn't update the checkbox input. So it's not just me xP

I've Googled around a bit and found various methods for checking if a checkbox is checked however the following methods fail:

if ($(elem).attr("checked")) // The original.
if ($(elem).attr("checked") == true)
if ($(elem).is(":checked"))
if ($(elem).checked)
// and:
var check = $(elem).match(/checked=/);
if (check == null)

Words of guidance are very much appreciated~ :3

8条回答
相关推荐>>
2楼-- · 2019-01-22 07:08

Just do this:

$('#checkbox').prop('checked',true).uniform('refresh');
查看更多
家丑人穷心不美
3楼-- · 2019-01-22 07:08

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.

查看更多
登录 后发表回答