简体   繁体   English

计算已更改的单选按钮的总数

[英]Count Total Number of Radio Button Changed

I have rows of radio buttons with a default check on one of the radio button on each row. 我有一排单选按钮,并且默认选中每一行上的一个单选按钮。

I would like to count the total radio button that gets changed after the page loads. 我想计算页面加载后更改的总单选按钮。 (Not total number of times the radio button is clicked) (不是单选按钮被单击的总次数)


rd1 rd2 rd3 rd4 rd5 rd1 rd2 rd3 rd4 rd5

rd6 rd7 rd8 rd9 rd10 rd6 rd7 rd8 rd9 rd10

rd11 rd12 rd13 rd14 rd15 rd11 rd12 rd13 rd14 rd15


rd1, rd6, rd11 is checked by default on page loads, so when a user now checks rd2 the total should say 1, if he checks rd8 next total should say 2, if he checks rd3, the total should remain as 2, if the user checks rd15 total should say 3, if he checks rd11 total should still say 3 默认情况下,页面加载时会检查rd1,rd6,rd11,因此,当用户现在检查rd2时,总数应为1,如果他检查rd8下一个总数应为2,如果他检查rd3,则总数应保持为2,如果用户检查rd15总数应说3,如果他检查rd11总数仍应说3

JSFIDDLE 的jsfiddle

$("input:radio").click(function() {
var totalRd = $('input:radio:checked').length;
$("#totalRd .rd-count").html(totalRd);
});

It is possible to check the radio button by default without the "check" word in the input tag. 默认情况下,可以在输入标签中不带“ check”字样的情况下检查单选按钮。

You can filter it, following your example: 您可以按照示例进行过滤:

DEMO jsFiddle 演示jsFiddle

$("input:radio").click(function () {
    var totalRd = $('table').find(':not(.pend) > input:radio:checked').length;
    $("#totalRd .rd-count").html(totalRd);
});
var totalChercks=0;
 $('input:radio').each(function() {
      if($(this).is(':checked')) {
         totalChercks+=1;
      }
      else {
          totalChercks=totalChercks;
      }
    });

You can use psudo class. 您可以使用psudo类。 Also added anonymous function for clarity.. 还添加了匿名功能以使内容更清晰。

http://jsfiddle.net/jFIT/jj4Uv/3/ http://jsfiddle.net/jFIT/jj4Uv/3/

$.fn.changedChecker = function () {
 $(this).click(function () {     
$(this).closest('tr').find('.changedRad').removeClass('changedRad');
    $(this).addClass('changedRad');
});
}

$("input:radio").changedChecker();

$('input[type=button]').click(function () {
 alert($('.changedRad').length);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM