I have a table as follows and with radio buttons to select the Table Row.

<div class="table-responsive">
            <table class="table">
              <tbody>
                <tr>
                  <td>
                    <label class="radio">
                      <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" data-toggle="radio">
                    </label>
                  </td>
                  <td class="text-center"><img class="img-rounded img-responsive" alt="exaple-image" src="images/covers/02.jpg"></td>
                </tr>
                <tr class="selected-row">
                  <td>
                    <label class="radio">
                      <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" data-toggle="radio" checked>
                    </label>
                  </td>
                  <td class="text-center"><img class="img-rounded img-responsive" alt="exaple-image" src="images/covers/01.jpg"></td>
                </tr>
                <tr>
                  <td>
                    <label class="radio">
                      <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" data-toggle="radio">
                    </label>
                  </td>
                  <td class="text-center"><img class="img-rounded img-responsive" alt="exaple-image" src="images/covers/03.jpg"></td>
                </tr>
                <tr>
                  <td>
                    <label class="radio">
                      <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" data-toggle="radio">
                    </label>
                  </td>
                  <td class="text-center"><img class="img-rounded img-responsive" alt="exaple-image" src="images/covers/04.jpg"></td>
                </tr>
                <tr>
                  <td>
                    <label class="radio">
                      <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" data-toggle="radio">
                    </label>
                  </td>
                  <td class="text-center"><img class="img-rounded img-responsive" alt="exaple-image" src="images/covers/05.jpg"></td>
                </tr>
              </tbody>
            </table>
          </div>

Now when i select a a Radio Button i wanna highlight that row and remove highlight from the other rows.

// Table: Add class row selected
$('.table tbody :radio').on('check uncheck toggle', function (e) {
    var $this = $(this),
        check = $this.prop('checked'),
        toggle = e.type == 'toggle',
        checkboxes = $('.table tbody :radio'),
        checkAll = checkboxes.length == checkboxes.filter(':checked').length

        $this.closest('tr')[check ? 'addClass' : 'removeClass']('selected-row');
    if (toggle) $this.closest('.table').find('.toggle-all :radio').checkbox(checkAll ? 'check' : 'uncheck');
});

Error:

It highlights the selected row but doesnt remove hightlight from previously selected row.

有帮助吗?

解决方案

Try this:

// Table: Add class row selected
$('.table tbody :radio').change(function() {
    $('.table tbody :radio:checked').closest('tr').addClass('selected-row');
    $('.table tbody :radio:not(:checked)').closest('tr').removeClass('selected-row');
});

其他提示

Using this can also work.

$('.table tbody :radio').on('click',function(){
    $(this).closest("tr").addClass("selected-row");
    $(".table tbody :radio").not(this).closest("tr").removeClass("selected-row")
});

Fiddle

Throwing my two-cents out on the table.

You can add a class to the closest tr (which from your example would be the radio buttons parent) and then remove classes from the closest tr siblings.

$('.table').on('change', ':radio', function() {
    $(this).closest('tr').addClass('selected-row').siblings('tr').removeClass('selected-row'); 
});

JSFiddle

Try using this

$('#element').click(function() {
  if($('#radio_button').is(':checked')) { alert("it's checked"); }
  });
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top