我有一组单选按钮,它们是在jsp中使用for循环动态创建的。我需要默认选中第一个单选按钮。如何通过jquery做到这一点?
<div id="groupATypeRadio">
<% for(SomeList list: someList .values()) {%>
<span class="font-small pull-left marRight5"><%= list.label %></span>
<input type="radio" class="marRight5 pull-left"name="groupA" id="<%= list.id%>" value="<%= list.getId()%>">
<%} %>
</div>
<div id="groupBTypeRadio">
<% for(SomeList list: someList .values()) {%>
<span class="font-small pull-left marRight5"><%= list.label %></span>
<input type="radio" class="marRight5 pull-left"name="groupB" id="<%= list.id%>" value="<%= list.getId()%>">
<%} %>
</div>
<div id="groupCTypeRadio">
<% for(SomeList list: someList .values()) {%>
<span class="font-small pull-left marRight5"><%= list.label %></span>
<input type="radio" class="marRight5 pull-left"name="groupC" id="<%= list.id%>" value="<%= list.getId()%>">
<%} %>
</div>
我需要从每个组中获取第一个单选按钮,并通过jquery进行检查。我怎样才能做到这一点?
将一个类添加到您的单选按钮容器div中,class="radio-groups"
然后使用以下方法检查第一个单选按钮
$(document).ready(function(){
$(".radio-groups").each(function() {
$(this).find("input[type='radio']").first().prop('checked', true);
});
});
请参阅下面的演示
$(".radio-groups").each(function() {
$(this).find("input[type='radio']").first().prop('checked', true);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="groupATypeRadio" class="radio-groups">
<span class="font-small pull-left marRight5">1</span>
<input type="radio" class="marRight5 pull-left" name="groupA" id="" value="">
<input type="radio" class="marRight5 pull-left" name="groupA" id="" value="">
<input type="radio" class="marRight5 pull-left" name="groupA" id="" value="">
<input type="radio" class="marRight5 pull-left" name="groupA" id="" value="">
</div>
<div id="groupBTypeRadio" class="radio-groups">
<span class="font-small pull-left marRight5">2</span>
<input type="radio" class="marRight5 pull-left" name="groupB" id="" value="">
<input type="radio" class="marRight5 pull-left" name="groupB" id="" value="">
<input type="radio" class="marRight5 pull-left" name="groupB" id="" value="">
<input type="radio" class="marRight5 pull-left" name="groupB" id="" value="">
</div>
<div id="groupCTypeRadio" class="radio-groups">
<span class="font-small pull-left marRight5">3</span>
<input type="radio" class="marRight5 pull-left" name="groupC" id="" value="">
<input type="radio" class="marRight5 pull-left" name="groupC" id="" value="">
<input type="radio" class="marRight5 pull-left" name="groupC" id="" value="">
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句