【问题标题】:simple form duplicate input简单表单重复输入
【发布时间】:2016-02-16 06:41:50
【问题描述】:

我正在使用 ruby​​ simple_form 生成一个带有复选框的表单。表单呈现为:

<%= simple_form_for(@user) do |f| %>
  <%= f.input :receive_notifications, as: :boolean, label: 'Receive Notifications' %>
<% end %>

这会输出 html:

<form accept-charset="UTF-8" action="/admin/users/9" class="simple_form edit_user" id="edit_user_9" method="post" novalidate="novalidate">
  <div style="display:none">
    <input name="utf8" type="hidden" value="✓">
    <input name="_method" type="hidden" value="patch">
    <input name="authenticity_token" type="hidden" value="m2rM8t/lZ1g3UfYER4xFiNkHKpDX5GrpjdapQCLPKHQ=">
  </div>
  <div class="form-group boolean optional user_receive_notifications">
    <input name="user[receive_notifications]" type="hidden" value="0">
    <label class="boolean optional control-label checkbox" for="receive_notifications">
      <input checked="checked" class="boolean optional" id="user_receive_notifications" name="user[receive_notifications]" type="checkbox" value="1">Receive Notifications
    </label>
  </div>
</form>

问题是,如何去除表单组内的重复输入?

编辑: 来自浏览器检查器的表单数据:

utf8:✓

_method:补丁

authenticity_token:m2rM8t/lZ1g3UfYER4xFiNkHKpDX5GrpjdapQCLPKHQ=

用户[receive_notifications]:0

用户[receive_notifications]:1

【问题讨论】:

    标签: ruby-on-rails ruby simple-form


    【解决方案1】:

    你不知道也不应该这样做。这是一个隐藏值,用于解决浏览器执行未填写复选框的方式的问题。

    问题是这样的:如果你只有一个(可见的)复选框,然后你没有点击它...... html 根本不会为那个复选框发送值。

    这不是您想要的……您想要的是,如果用户不点击复选框,您会收到以下值::user_receive_notifications =&gt; false

    所以这就是 Rails 破解 html 的方式,以便在页面上存在复选框但未选中时始终发送该值。

    【讨论】:

    • 我的表单正在提交 2 个具有不同值且名称相同的字段。
    • 对...那么您有问题,但请相信我 - 隐藏字段不是导致该问题的原因。那么,让我们看看表格的其余部分?
    • 另外 - 你为什么不向我们展示一个你在使用它时收到的数据的例子:) 注意:你不是新来的,所以我应该说:不要将代码放在 cmets 中(就像这里一样),因为格式真的很糟糕。您应该编辑您的问题并将代码放在那里。
    猜你喜欢
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    相关资源
    最近更新 更多