2013-07-15 59 views
0

我想在導軌中以簡單形式使用我的自定義按鈕作爲單選按鈕。但我真的不知道該怎麼做。我有一個CSS代碼,給我我想要的按鈕,但我認爲我的簡單形式是錯誤的,我真的不知道如何設置javascrip ...Rails: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按鈕:

.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; 
} 

回答

1

看到這個註釋代碼:

// 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); 

    }); 

更新

因爲當你有simple_form_for @something@something如果來自類Something,f.input :some_field輸入產生與ID:something_some_field,你可以檢查你生成的HTML來確認。

+0

謝謝,它工作:)但爲什麼'#post_type'? – allegutta

+0

看更新..... – juanpastas

+0

啊,我明白了。再次感謝 :) – allegutta