jQuery检查是否选择了单选按钮

时间:2014-03-17 22:22:19

标签: jquery html radio-button checked

我有一个如下表格,并使用单选按钮选择表格行。

<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>

现在当我选择一个单选按钮时,我想要突出显示该行并从其他行中删除高亮显示。

// 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');
});

错误:

  

它会突出显示所选行,但不会从中删除高光   以前选择的行。

4 个答案:

答案 0 :(得分:2)

试试这个:

// 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');
});

答案 1 :(得分:1)

使用它也可以。

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

Fiddle

答案 2 :(得分:1)

把我的两美分扔在桌子上。

您可以将类添加到最近的tr(从您的示例中将是单选按钮父级),然后从最近的tr兄弟中删除类。

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

JSFiddle

答案 3 :(得分:0)

尝试使用此

$('#element').click(function() {
  if($('#radio_button').is(':checked')) { alert("it's checked"); }
  });