Jquery:如果选中单选按钮,则将类添加到容器

时间:2016-08-10 10:29:40

标签: jquery

如果选中内部单选按钮,如何将类active添加到元素payment--method

.method--description{
  display: none;
}

.payment--method.active .method--description{
  display: block;
}
<div class="payment--method">

  <div class="method--input">
    <input type="radio" name="payment" value="1" id="payment_option1" checked="checked">
  </div>

  <div class="method--label">
    <label for="payment_option1">Option 1</label>
  </div>

  <div class="method--description">
    Option 1 description
  </div>

</div>

<div class="payment--method">

  <div class="method--input">
    <input type="radio" name="payment" value="2" id="payment_option2">
  </div>

  <div class="method--label">
    <label for="payment_option2">Option 2</label>
  </div>

  <div class="method--description">
    Option 2 description
  </div>

</div>

1 个答案:

答案 0 :(得分:2)

请查看下面的代码段。

&#13;
&#13;
Version version = Loader.readVersion(inputStream); // Instantiates a new Version

if (version.getMajor() != 2)
    throw new IOException("Only major version 2 is supported");
&#13;
Loader.readVersion()
&#13;
$(document).ready(function(){
  makeActive();
  $("input[type='radio']").on("change",function(){
    makeActive();
  });
});
function makeActive(){
  $("input[type='radio']").each(function(){
      if($(this).prop("checked")){
        $(this).closest('.payment--method').addClass("active");
      }else{
        $(this).closest('.payment--method').removeClass("active");
      }
    });
}
&#13;
&#13;
&#13;