如何更改rails中的复选框?

时间:2014-09-23 16:09:52

标签: javascript ruby-on-rails checkbox

如何将复选框设置为true或false ..我已尝试过所有这些组合:

<%= check_box_tag 'prefs[can_be_email_notified]', :id => 'user_preferences_can_email'%> Email 
<%= check_box_tag 'prefs[can_be_email_notified]', '', :id => 'user_preferences_can_email'%> Email
<%= check_box_tag 'prefs[can_be_email_notified]', '', :checked =>'',:id => 'user_preferences_can_email'%> Email

尝试使用这些

更改复选框
document.getElementById("user_preferences_can_email").checked = true;
document.getElementById("user_preferences_can_email").value = true;
document.getElementById("user_preferences_can_email").checked.value = true;

也许我不清楚......

我有一组设置为按钮的值,并希望更改要在按钮上设置的值上取消选中或取消选中的复选框,

即:

function doubleClickUser(can_email, can_screen) {
        document.getElementById("user_preferences_can_email").checked = can_email;
        document.getElementById("user_preferences_can_screen").checked = can_screen;
}

<button class="button" onclick="doubleClickUser('true','false')"><%= user[:email] %></button>

<%= check_box_tag 'prefs[can_be_email_notified]', :id => 'user_preferences_can_email'%> Email
<%= check_box_tag 'prefs[can_be_screen_notified]', :id => 'user_preferences_can_screen' %> Screen

3 个答案:

答案 0 :(得分:0)

如果您想要检查,请尝试:

EMAIL
<%= check_box_tag "email",params[:email].to_s  , {:checked => true}  %>

此外,如果你想要一个值,你可以试试这个:

<%= check_box_tag "email", "5", params[:email].to_s == "5", {:checked => true}  %>

答案 1 :(得分:0)

在控制器或模型中设置字段的值,然后只需在标准的Rails表单中包含该元素:

# controller
@pref = Pref.new (or Pref.find(params[:id])

# view
<%= form_for @pref do |f| %>
  <%= f.label :can_be_email_notified %>
  <%= f.check_box :can_be_email_notified %>
<% end %>

这将反映can_be_email_notified @pref的任何值。

答案 2 :(得分:0)

想出来,你必须先将状态设置为false

<%= check_box_tag 'prefs[can_be_email_notified]', '', false, :id => 'user_preferences_can_email' %> Email <br>
<%= check_box_tag 'prefs[can_be_screen_notified]', '', false, :id => 'user_preferences_can_screen' %> Screen <br>
<%= check_box_tag 'prefs[can_be_sms_notified]', '', false, :id => 'user_preferences_can_sms' %> Text </div></th>