Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
734 views
in Technique[技术] by (71.8m points)

ajax - jQuery Uniform Checkbox does not (un)check

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...