代码之家  ›  专栏  ›  技术社区  ›  Birdie Golden

单选按钮-初始检查,然后单击检查

  •  1
  • Birdie Golden  · 技术社区  · 7 年前

    现在我让它工作,如果人们点击收音机,它会突出显示标签。我想它被突出显示在页面加载以及。

    如果选择了该收音机,是否有办法突出显示页面加载时的标签?

    $('.subscription-option input').click(function() {
        $('label').removeClass("highlight-this");
        if($(this).is(':checked')) {
            $(this).parent().addClass('highlight-this');
        } 
    });
    

    https://www.solonutrition.com/product/dark-chocolate-mandarin/

    1 回复  |  直到 7 年前
        1
  •  1
  •   Birdie Golden    7 年前

    尝试另一种方法:

    var paintLabel = function paintLabel()
    {
      $('label').removeClass("highlight-this");
      $('input:checked').parent().addClass('highlight-this');
    };
    
    $('input').click(paintLabel);
    
    paintLabel();
    .highlight-this {
      background-color: #aa0
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <label>
      <input type="radio" name="rdb"> Rdb1
    </label>
    <label>
      <input type="radio" name="rdb" checked="checked"> Rdb2
    </label>
    <label>
      <input type="radio" name="rdb"> Rdb3
    </label>