[英]rails default selected radio button
I have following code for radio options in loop. 我有以下代码用于循环中的单选选项。
<% @this.each do |p| %>
<div class="radio_button_with_text">
<%=radio_button "this" ,"name",p.name ,:onclick=>"save_package(#{p.id})"%>
<p class="radioText"><%= p.name %></p>
</div>
<% end %>
I wish to have the first radio option default selected. 我希望选择默认的第一个无线电选项。 How can I do this? 我怎样才能做到这一点?
:checked => this.first
is not wokring for me. 不为我工作。
Try this Example: 试试这个例子:
<% @products.each do |product| %>
<div class="radio_button_with_text">
<%=radio_button_tag product.name ,:id, @products.first == product, class: "save_package", "data-id" => product.id %>
<p class="radioText"><%= product.name %></p>
</div>
<% end %>
$(document).on('click', '.save_package', function(e) {
save_package(this.attr("data-id"));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.