jQuery - recheck checkboxes after clearing them with remoteAttr()

给你一囗甜甜゛ 提交于 2019-12-23 23:04:56

问题


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.


回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!