【问题标题】:Selected values radio_button rails选定的值 radio_button rails
【发布时间】:2016-09-12 02:54:08
【问题描述】:

如何获取选中元素的值。并将其保存在变量中以在跨度中显示。我试图直接保存到数据库。数据库也不保存选定的值。

...
<%= f.collection_radio_buttons( :route_id, Route.all, :id, :time_trip, {}, {name: "timeselect", class: "timeselect"})  %>
... 
 <span id="timevalue"></span>

jQuery

(document).ready(function () {

$('input[name="timeselect"]:radio').change(function(){

        var str3="";
        $('input[name="timeselect"]:checked').val();
            str3 += $(this).text() + "\n";

        $("#timevalue").text();
    });
});

【问题讨论】:

    标签: ruby-on-rails radio-button form-for


    【解决方案1】:

    仅仅为了能够选择而覆盖名称选项并不是一个好主意,因为它会影响参数。

    在 Rails 中使用表单构建器时:

    <%= form_for(Foo.new) %>
      <%= f.collection_radio_buttons( :route_id, Route.all, :id, :time_trip, {}, { class: "timeselect" })  %>
    <% end %>
    

    这将为您提供以下参数哈希:

    foo: {
      route_id: 1
    }
    

    “root”键是通过在绑定到表单构建器的模型上调用 .model_name 来推断的。

    这些参数可以是简单的白名单强参数:

    params.require(:foo).permit(:route_id)
    

    您可以通过以下方式选择元素:

    $('input[name="foo[route_id]"]')
    

    或者通过 ID - 检查每一个的最简单方法是查看 HTML 输出或使用浏览器检查器。

    【讨论】:

    • 数据库保存,但$('input[name="orders[route_id]"]:radio').change(function(){ var str3=""; $('input[name="orders[route_id]"]:checked').val(); str3 += $(this).text() + "\n"; $("#timevalue").text(str3) });不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 2013-10-01
    • 2014-12-12
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多