问题
if I reset a bunch of input:checkbox fields with resetAttr(), which is working perfectly, I'm unable to recheck those checkboxes using the data of an ajax-response in combination with .attr('checked','true').
So the workflow of the script should be like this:
Get some data during an ajax-request. If this is successfull, continue.
When opening the form, reset all of it's content, including the checkboxes. That isn't working with
document.getElementById("ID").reset();
alone. Therefore I usedremoveAttr();
Now take the data of the ajax-request and put it into the DOM using
jQuery('#SomeID').find('input[value="'+SomeObject[key]+'"]').attr('checked','true');
Without having used remoteAttr() before the form will be filled as described in Step 3. Using removeAttr() leads to the result, that refilling the checkboxes will be ignored.
Any suggestions? Best regards Ralf
UPDATE: I found a solution, which is a little nasty but working:
First I remove the checked attributes with:
jQuery('#form').find('input:checkbox').attr('checked',false);
And then reset the form once again, using:
document.getElementById("such-sets-_viewSuchSets-form").reset();
After that I refill the form with checked checkboxes, textboxes etc. using jQuery().val() etc.
回答1:
For whom who are searching solution for this problem. Seems it is because of the "attr" function does not update the status of checkbox.
Please try to change:
$('.someClass').attr("checked", true);
into
$('.someClass').prop("checked", true);
For detail please visit the offical document: http://api.jquery.com/prop/
回答2:
Why don't you run this when opening the form aswell, but setting all to false
instead?
jQuery('#SomeID').find('input[value="'+SomeObject[key]+'"]').attr('checked',false);
来源:https://stackoverflow.com/questions/9065931/jquery-recheck-checkboxes-after-clearing-them-with-remoteattr