Jquery function to toggle all radio buttons on the page as

我的梦境 提交于 2019-12-13 02:56:14

问题


The question sums it up ... Here's what I've tried, is there a better way? It works once (selects and deselects once) then stops working ...

<a href="#resources" onclick="$(':radio').each(function(){$(this).attr('checked', true); });">(Select All)</a>
<a href="#resources" onclick="$(':radio').each(function(){$(this).attr('checked', false); });">(Deselect All)</a>

回答1:


For stuff like this, I recommend:

$('input:radio').each(function() { $(this).prop('checked', false); });
$('input:radio').each(function() { $(this).prop('checked', true); });



回答2:


You can do it like this:

$("#selectAll").click(function() { $('input:radio').prop('checked', true);  });

$("#deselectAll").click(function() { $('input:radio').prop('checked', false); });

No need to use .each function, since jQuery uses implicit iteration anyway. Here is working jsfiddle



来源:https://stackoverflow.com/questions/25334642/jquery-function-to-toggle-all-radio-buttons-on-the-page-as

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