单击td,在jQuery中选择单选按钮
对于一些非常基本的jQuery有一个小问题,我希望能够单击一个表单元格,然后让它自动选择里面的单选按钮 HTML: 非常感谢您的帮助,谢谢 使用此选择器:单击td,在jQuery中选择单选按钮,jquery,button,radio,Jquery,Button,Radio,对于一些非常基本的jQuery有一个小问题,我希望能够单击一个表单元格,然后让它自动选择里面的单选按钮 HTML: 非常感谢您的帮助,谢谢 使用此选择器: $('input:radio', this).attr('checked', true); 或使用find方法: $(this).find('input:radio').attr('checked', true); 以下是您的代码的外观: $("td").click(function () { $(this).find('input
$('input:radio', this).attr('checked', true);
或使用find
方法:
$(this).find('input:radio').attr('checked', true);
以下是您的代码的外观:
$("td").click(function () {
$(this).find('input:radio').attr('checked', true);
});
试一试
尝试查找而不是最近的
$(this).find('input:radio').attr('checked',true);
这很好用
$(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;
}
});
});
当用作上下文时,无需在jQuery构造函数中包装此,
$('input:radio',this)
就足够了。非常感谢!不过有一件事很快,我会在选中单选按钮时更改类,但是当您单击单元格时,即使选中单选按钮,这也不起作用?$(“td-input[type=radio]”).bind('change-click',function(){$('td').removeClass('selected');$(this).parent('td').addClass('selected');})@第9栏:更新以避免任何进一步的混淆:)@Nick:你应该将其作为另一个问题发布:)既然答案已经正确,我就在这里添加。最近的()
用于向上遍历DOM树,例如获取
或
,而不是向下遍历。
$(this).find('input:radio').attr('checked','checked');
$(this).find('input:radio').attr('checked',true);
$(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;
}
});
});