7

一些非常基本的 jQuery 有一个小问题,我希望能够单击表格单元格,然后让它自动选择里面的单选按钮。

HTML:

  <table>
   <tr>
    <td>
     1<br>
     <input type="radio" name="choice" value="1">
    </td>
    <td>
     2<br>
     <input type="radio" name="choice" value="2">
    </td>
    <td>
     3<br>
     <input type="radio" name="choice" value="3">
    </td>
   </tr>
  </table>

jQuery:

 $("td").click(function () {

  $(this).closest('input:radio').attr('checked',true);

 });

任何帮助将不胜感激,谢谢!

4

4 回答 4

14

使用这个选择器:

$('input:radio', this).attr('checked', true);

或使用find方法:

$(this).find('input:radio').attr('checked', true);

您的代码应如下所示:

$("td").click(function () {
   $(this).find('input:radio').attr('checked', true);
});
于 2011-01-07T11:31:56.723 回答
2

尝试

$(this).find('input:radio').attr('checked','checked');
于 2011-01-07T11:33:30.597 回答
2

尝试查找而不是最接近的。

$(this).find('input:radio').attr('checked',true);
于 2011-01-07T11:38:08.200 回答
1

这完美地工作

 $(function () {
        $('td').click(function () {

        var cell = $(this),
            state = cell.data('state') || 'first';

        switch (state) {
            case 'first':
                cell.data('state', 'second');
                cell.find('input:radio').attr('checked', true);
                cell.find('input:radio').data('checked', true);
                cell.find('input:radio').prop('checked', true);
                break;
            case 'second':
                cell.data('state', 'first');
                cell.find('input:radio').attr('checked', false);
                cell.find('input:radio').data('checked', false);
                cell.find('input:radio').prop('checked', false);
                break;
            default:

                break;
        }
    });
});
于 2019-02-27T01:18:24.340 回答