如何在jQuery中预先选择一个单选按钮

我想预先选择以下集合中的第一个单选按钮:

HTML

  

jQuery的

 var i = 1; // for sake of this example $('[name="serviceNoteType\\['+ i +'\\]"]').prop('checked', true); 

结果是第二个单选按钮被选中,我希望选择第一个单选按钮。 如何定位第一个单选按钮?

您可以将.first()添加到您的选择器:

 var i = 1; // for sake of this example $('[name="serviceNoteType\\['+ i +'\\]"]').first().prop('checked', true); 
    

更短的forms。 试试这个:

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

名称需要不同。 您的选择器只会检查该名称的最后一个元素。

 $('[name="serviceNoteType\\['+ i +'\\]"]:first-child').prop('checked', true); 

未经测试,因此语法可能稍微偏离,但应该给你的想法。