简体   繁体   English

Rails:simple_form单选按钮

[英]Rails: simple_form radio buttons

I want to use my custom button as radio buttons in a simple form in rails. 我想以简单的形式将我的自定义按钮用作单选按钮。 But I really don't know how to do this. 但是我真的不知道该怎么做。 I have a CSS code that gives me the button I want, but I think my simple form is wrong and I don't really know how to set up the javascrip... 我有一个CSS代码,可以给我想要的按钮,但是我认为我的简单表单是错误的,我真的不知道如何设置javascrip ...

Does someone know how I can get this right? 有人知道我该怎么做吗?

My simple form: 我的简单形式:

  <%= simple_form_for(@post, :html => {:class => 'form-inline' }) do |f| %>
    <%= f.input :link, label: false %>
    <div class="button_group">
      <%= f.input :type, as: :hidden %>
      <a class="radio_button" data-value="0">ONE</a>
      <a class="radio_button" data-value="1">TWO</a>
      <a class="radio_button" data-value="2">THREE</a>
    </div>
    <%= button_tag(type: 'submit', class: "submit_button") do %>
      SUBMIT!
    <% end %>
  <% end %>

CSS button: CSS按钮:

.radio_button {
    color: white;
    background-color:#828282;
    font-family: 'Fjalla One', serif;
    font-size: 10pt;
    font-weight: 100;
    text-align: center;
    font-style: italic;
    padding:0px 6px;
    padding-right: 8px;
    padding-top: 3px;
    padding-bottom: 2px;
    text-decoration:none;
    border: none;
}.radio_button:hover {
    background-color:#0E9EBB;
    color: white;
    text-decoration:none;
}.radio_button:active {
    position:relative;
    background-color:#0E9EBB;
    top:1px;
}

See this commented code: 请参阅以下注释代码:

// select all next .radio_button elements to #post_type hidden input
$('#post_type ~ a.radio_button').

  // execute code block on click
  click( function(){

     // this refers to clicked a element, you will get strings like: 0, 1, 2
     var clickedValue = $(this).data('value');

     // set clicked value in hidden input
     $('#post_type').val(clickedValue);

  } );

Update 更新

because when you have simple_form_for @something and @something if from class Something , f.input :some_field input is generated with id: something_some_field , you can check you generated HTML to confirm. 因为当您拥有simple_form_for @something@something如果来自Something类), f.input :some_field输入是使用id: something_some_field生成的,因此您可以检查生成的HTML以进行确认。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用simple_form基于无线电选择隐藏/显示字段 - Hiding/Showing Fields Based On Radio Selection With simple_form 如何将Javascript验证添加到Rails simple_form? - How to add Javascript validations to Rails simple_form? 使用可点击的div在Rails中提交simple_form - Using a clickable div to submit a simple_form in Rails Ruby on Rails,simple_form - 我想从 html select_tag 字段设置 simple_form 的隐藏字段 - Ruby on Rails, simple_form - I want to set a hidden field of the simple_form from a html select_tag field simple_form提示作为工具提示 - simple_form hints as tooltip 基于单选按钮选择的字段切换,带有simple_form,haml和jquery - Toggle fields based on radio button selection with simple_form, haml, and jquery Rails + simple_form + remote_true +自定义控制器:如何处理“ new”和“ edit”的不同表单动作? - Rails + simple_form + remote_true + custom controller: how to handle different form actions for 'new' and 'edit'? Rails 5 ajax 表单:单击 Ajax 中的 2 个单选按钮之一提交表单 - Rails 5 ajax form: submit form on clicking one of the 2 radio buttons in Ajax 最接近的div作为Rails和Coffee脚本以及simple_form中的引导日期选择器的容器 - Closest div as container for bootstrap datepicker in rails and coffee script and simple_form 为什么simple_form视图中的复选框在rails 3.2中被选中或取消选中时总是返回1? - Why the check box in simple_form view always returns 1 when being checked or unchecked in rails 3.2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM