jquery - 使用 jquery 的单选按钮

标签 jquery

我有一堆 div:

<div class='option'>
    <span>here is a label for one pair of radio buttons</span>
    <a href='#' rel='radio' class='on'>On</a>
    <a href='#' rel='radio' class='off'>Off</a>
</div>

我正在尝试使用 jQuery 在这两个类(“off”和“on”)之间切换。

<script>; 
$(document).ready(function(){ 
   $("a[rel='radio']").click(function(){
      if($(this).attr('class')=='on'){
        //make $(this) have class 'off'
        //make the other a in *this* div have class 'on'

      } else {
        //make $(this) have class 'on'
        //make the other a in the div have class 'off'
      }

  });
});
</script>

如何将注释的伪代码转换为现实世界的 jQuery?

最佳答案

您描述的行为与标准单选按钮不同,但可以如下所示:http://jsfiddle.net/BJGKH/ .

$("a[rel='radio']").click(function(){
   // this and sibling - toggle both classes from each
   $(this).siblings("a[rel='radio']").addBack().toggleClass("on off");
});​

关于jquery - 使用 jquery 的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12167305/

相关文章:

javascript - 防止 X 个数字出现在选项中

jquery - 使用 jquery 在同一 div 中查找类

javascript - 添加域名搜索ajax表单的最佳方式

javascript - javascript 的 Jquery 参数替代方案

javascript - 检查数组中的值(包含不是函数)

jQuery - 为什么附加的 Div 总是在最上面?

javascript - jquery如何显示不同颜色的按钮

javascript - 使用 JQuery 删除文本并更改嵌入在 div 中的 div 中的 bgimage

javascript - 是否可以使用 jQuery.attr() 函数设置多个数据属性?

javascript - 带有子菜单的 Bootstrap 导航栏 - 如何删除 768 下的类 "disabled"@media width