Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - recheck checkboxes after clearing them with remoteAttr()

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:

  1. Get some data during an ajax-request. If this is successfull, continue.

  2. 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 used removeAttr();

  3. 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.

like image 974
Ralf Marmorglatt Avatar asked Nov 19 '25 04:11

Ralf Marmorglatt


1 Answers

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/

like image 82
tommylpc Avatar answered Nov 21 '25 19:11

tommylpc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!