用jQuery计算选中的单选按钮 [英] Counting Checked Radio Buttons with jQuery

查看:66
本文介绍了用jQuery计算选中的单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有不同数量的单选按钮.这些按钮具有两个不同的类别,分别为yesno.当用户浏览页面时,我需要找出他们选择的每个班级有多少个,并将其显示在页面底部的<p>中.

I have pages with varying numbers of radio buttons. These buttons have two different classes, yes, and no. As the user goes down the page, I need to figure out how many of each class they have selected, and display it in a <p> at the bottom of the page.

当用户多次单击单选按钮时,我无法阻止将1添加到变量中.这是我的代码的JS小提琴:

I'm having problems preventing 1 getting added to the variable when a user clicks on a radio button more than once. Here's a JS fiddle with my code:

http://jsfiddle.net/AnWU3/3/

由于某种原因它根本无法工作,我不明白为什么.

It's not working at all for some reason, and I don't see why.

推荐答案

尝试以下操作:

$(document).ready(function() {
    $('input:radio').change(function(){
        var yes = $('.yes:checked').length
        var no = $('.no:checked').length
        $('.yes_results').text(yes)
        $('.no_results').text(no)                        
    })
});

演示

这篇关于用jQuery计算选中的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆