【问题标题】:RubyOnRails Form_for check_box_tag disabled Checkbox POST submitRubyOnRails Form_for check_box_tag disabled Checkbox POST submit
【发布时间】:2014-06-15 13:13:01
【问题描述】:

我创建了一个 check_box_tag 并将其设置为选中并禁用它已设置并且用户无法更改复选框的状态。

          <% checkedDisabled = false
         if current_user.id == user.id
           checkedDisabled = true
         end
      %>

      <div>
        <%= check_box_tag "book[user_ids][]", user.id, @book.users.include?(user), {checked: checkedDisabled, disabled: checkedDisabled}  %>
        <%= user.email %>
      </div>

当前用户==数组中的用户的复选框被选中并禁用

问题是,如果我提交表单,禁用的复选框不会被提交为选中状态。 我该如何解决这个问题。我使用只读,但您仍然可以取消选中该复选框(即使它被提交为选中状态。

我需要在提交之前从复选框中删除disabled 标签。

【问题讨论】:

  • 一种解决方案是在这些复选框上设置一个 javascript click 事件侦听器,如果它是 checked 并具有 readonly=true,它将是 return false

标签: html ruby-on-rails checkbox form-for


【解决方案1】:

禁用的表单字段不会提交,因此您不想使用它。在文本字段、选择框等上,您可以使用readonly 来获得所需的效果,但是对于复选框而言,这并不能正常工作。有关更多信息,请参阅此答案:Can HTML checkboxes be set to readonly?

解决方法是禁用复选框,然后将值存储在隐藏字段中,如下所示:

 <%= check_box_tag "book[user_ids][]", user.id, @book.users.include?(user), {checked: checkedDisabled, disabled: checkedDisabled}  %>
<% if checkedDisabled %>
 <%= hidden_field_tag "book[user_ids][]", user.id, value: @book.users.include?(user)  %>
<% end %>

【讨论】:

    猜你喜欢
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多