Javascript 如何设置第一个子元素&x27;s单选按钮已签入rails4?

Javascript 如何设置第一个子元素&x27;s单选按钮已签入rails4?,javascript,ruby-on-rails,Javascript,Ruby On Rails,我在用栏杆。我有一个下拉列表。我想检查第一个子元素的单选按钮。下拉列表中的项目是父项,在选择每个父项时,会显示一个列表,该列表是子项。所有子元素都通过循环显示,然后在javascript的帮助下,我只显示属于下拉列表中所选父项的子元素 模板 一旦更改父元素id,将调用“$”(“选择[id='parent'].change”),然后模板中的子元素为:- <div class="child<%= @parent.id %>" style="display:none">

我在用栏杆。我有一个下拉列表。我想检查第一个子元素的单选按钮。下拉列表中的项目是父项,在选择每个父项时,会显示一个列表,该列表是子项。所有子元素都通过循环显示,然后在javascript的帮助下,我只显示属于下拉列表中所选父项的子元素

模板 一旦更改父元素id,将调用“$”(“选择[id='parent'].change”),然后模板中的子元素为:-

<div class="child<%= @parent.id %>" style="display:none">           
    <%=radio_button :child_name, @parent.id, child.name ,id: "child"%><%=child.name%>

</div>

$("select[id='parent_<%=@parent.id%>']").change(function(){ 
    $(".child"+ this.value).each(function(){
        $(this).show();
        $(this).firstElementChild.setAttribute("checked","checked"); //Its not working
     });

});

$(“选择[id='parent.']”)。更改(函数(){
$(“.child”+this.value).each(函数(){
$(this.show();
$(this).firstElementChild.setAttribute(“已检查”、“已检查”);//它不起作用
});
});
试试这个:

// Instead
// $(this).firstElementChild.setAttribute("checked","checked"); 
$(this).children().first().attr("checked", true);

既然您正在循环,那么f.radio\u按钮worked!!谢谢:)