Jquery:选择表行时选择一个单选按钮

当用户点击表格行中的任何位置时,我需要选择一个单选按钮。 我一直在玩一些选项,但无法得到任何工作。

任何帮助表示赞赏。

这是一个示例表:

用这个:

 $('#tableSelect tr').click(function() { $(this).find('th input:radio').prop('checked', true); }) 

这是一个工作小提琴: http : //jsfiddle.net/mrchief/eFnL7/1/

编辑:为了更好(通过多少?)性能,使用input[type=radio]选择器: http : //jsfiddle.net/mrchief/eFnL7/2/

 $('#tableSelect tr').click(function(){ $(this).find('input[type=radio]').prop('checked', true); }); 

小提琴: http : //jsfiddle.net/8PzX2/

文档:

http://api.jquery.com/click/

http://api.jquery.com/prop/

 $('table tr').click(function(){ $(this).find('input[type=radio]').prop('checked', true); }); 
 $(function () { $('#tableSelect').delegate('tbody > tr', 'click', function () { $(this).find('input[name="UserSelected"]').prop('checked', true); }); }); 

您可以使用delegate执行此操作,这将仅将事件处理程序附加到表元素,并将适用于每个tr

  $(function (){ $('#tableSelect').delegate('tbody > tr', 'click', function(){ $(this).find('input[name="UserSelected"]').attr('checked', true); }); }); 
User Management
Richard Rice Rick IT rick Access Denied Suspended Shipping User Administrator Super Administrator Accounting Report Viewer
Mickey Hockenberry Mickey Purchasing mickeyh Access Denied Suspended Shipping User Administrator Super Administrator Accounting Report Viewer
Michael Lynch Mike Shipping mjl Access Denied Suspended Shipping User Administrator Super Administrator Accounting Report Viewer