如何在单选按钮上添加和删除类? [英] How to add and remove class on radio button?

查看:69
本文介绍了如何在单选按钮上添加和删除类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 JSFiddle 上发现这个时遇到了问题

I've problem when I found this one on JSFiddle

http://fiddle.jshell.net/pepesustaita/sdL2v/

有两个用于采样的收音机,但我需要更多的 2..

there has two radio for sampling, but I need that for more that 2..

 $(document).ready(function(){
    $('#radio1').click(function(){
        $('.what1').addClass('active1').siblings().removeClass('active');       
    });
    $('#radio2').click(function(){
        $('.what2').addClass('active').siblings().removeClass('active1');       
    });
});

如何为第三、第四等添加更多条件?

How to add more condition for 3rd, 4th, etc.. ?

推荐答案

如果我理解正确,您正在寻找比为每个元素分配单独的点击处理程序更通用的解决方案?也许是这样的:

If I understand you correctly, you are looking for a more generic solution than assigning individual click handlers to each element? Perhaps, something along these lines:

$(document).ready(function(){
    $('input[type="radio"]').click(function( ){
        $('input[type="radio"]').siblings().removeClass('active');
        $(this).next().next().addClass('active');
    });
});

这会将相同的点击处理程序附加到每个 radio 类型的输入.单击单选按钮时,活动类将从所有其他单选按钮中删除,然后将活动类添加到上次单击的单选按钮中.

This will attach the same click handler to each input of type radio. When a radio button is clicked, the active class is removed from all other radio buttons, and the active class is then added to the last clicked radio button.

小提琴:http://fiddle.jshell.net/adrv14of/

这篇关于如何在单选按钮上添加和删除类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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