简体   繁体   中英

Radio button to check all boxes

<table class="table borderless">
    <thead>
      <tr>
        <th>Rank</th>
        <th>+/-</th>
        <th>Searches</th>
        <th></th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td><input type="radio" name="optradio" class="custom"> ALL</td>
        <td><input type="radio" name="optradio" class="custom"> ALL</td>
        <td><input type="radio" name="optradio" class="custom"> ALL</td>
        <td><input id="ex2" type="text" class="span2" value="" data-slider-min="10" data-slider-max="1000" data-slider-step="1" data-slider-value="[0,1000]" data-slider-id="RC" id="R"/></td>
      </tr>
      <tr>
        <td><input type="checkbox" class="custom-selector"> Rank 1-10</td>
        <td><input type="checkbox" > Up</td>
        <td><input type="checkbox" > Over</td>
      </tr>
      <tr>
        <td><input type="checkbox" class="custom-selector"> Rank 11-20</td>
        <td><input type="checkbox" > Down</td>
        <td><input type="checkbox" > Under</td>
      </tr>
      <tr>
        <td><input type="checkbox" class="custom-selector"> Rank 21-100</td>
        <td><input type="checkbox" > No movement</td>

      </tr>
      <tr>
        <td><input type="checkbox" class="custom-selector"> Not in top 100</td>

      </tr>
    </tbody>
  </table>

Script:

$(document).ready(function () {
       $(".custom").change(function () {
              $(".custom").parent().find(".custom-selector").prop("checked", true);
       })
   });

What I need is a radio button that checks all the boxes when selected.

Right now nothing happens when I click the radio button. However, if I add this radio button:

<input type="radio" id="op5" value="1" name="options2" class="custom">

Then they both work. Even the initial one checks all the boxes.

What is this strange behavior? I'm missing something

EDIT: it appears the radio button must be outside the table? Can I have it inside somehow?

try:

$(".custom").closest("tbody").find(".custom-selector").prop("checked", true);

or Just:

$(".custom-selector").prop("checked", true);

Try this:

$(".custom").change(function () {
    $(this).parents('tbody').find(".custom-selector:checked");
});

and some reference: https://api.jquery.com/checked-selector

eventually remove your action selector using .not(this)

Try this :

    $(document).ready(function () {
           $(".custom").change(function () {
             $(".custom-selector").prop("checked", true);
        })
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM