【问题标题】:Jquery function to toggle all radio buttons on the page asJquery 函数将页面上的所有单选按钮切换为
【发布时间】:2014-10-09 16:11:22
【问题描述】:

问题总结了......这是我尝试过的,有没有更好的方法?它工作一次(选择和取消选择一次)然后停止工作......

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

【问题讨论】:

标签: jquery html css button radio


【解决方案1】:

对于这样的东西,我推荐:

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

【讨论】:

    【解决方案2】:

    你可以这样做:

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

    不需要使用.each 函数,因为jQuery 无论如何都使用隐式迭代。这是工作jsfiddle

    【讨论】:

      猜你喜欢
      • 2020-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-15
      • 1970-01-01
      • 2012-01-08
      • 2014-09-10
      相关资源
      最近更新 更多