【问题标题】:<div> breaks <form><div> 打破 <form>
【发布时间】:2014-02-16 15:56:33
【问题描述】:

我有以下代码来生成&lt;form&gt;

  <div class="modal-dialog">
<div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title" id="myModalLabel">My form</h4>
  </div>
  <div class="modal-body">
   <ul class="errors has-error"></ul>
   <p>Some text</p>
    <%= form_tag(some_path(@object), remote: true, id: "my-form") do %>
        <table class="table table-striped show">
          <tr>
            <th></th>
            <th>Some value</th>
            <th>Some value2</th>
            <th>Some value3</th>
            <th>Some value4</th>
            <th>Some value5</th>
            <th>Some value6</th>
            <th>Some value7</th>
          </tr>
            <% @object.each do |o| %>
              <tr>
                <td><div class="checkbox"><label><%= check_box :condition1, o.value1 %></label></div></td>
                <td><%= o.value2.strftime("%d.%m.%Y") %></td>
                <td><%= o.value3 %></td>
                <td><%= o.value4 %></td>
                <td><%= number_with_precision o.value5, :precision => 2 %></td>
                <td><%= number_with_precision o.value6, :precision => 2 %></td>
                <td><%= number_with_precision o.value7, :precision => 2 %></td>
                <td><%= number_with_precision o.value8.strftime("%d.%m.%Y") %></td>
              </tr>
            <% end %>


        </table>

    <p>Some text.</p>

  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
    <%= submit_tag "Submit", data: { disable_with: "Please wait..." }, :id => 'modal-submit', :class => "btn btn-primary" %>
    <% end %>
    </div>
</div>

奇怪的是,在生成的 HTML 代码中,提交按钮放在 &lt;/form&gt; 标记之后。但是,如果我在&lt;/table&gt; 之后的第一个&lt;/div&gt; 之前放置提交按钮,则&lt;input class="btn btn-primary" data-disable-with="Please wait..." id="modal-submit" name="commit" type="submit" value="Submit"&gt; 在表单内。这是预期的行为还是表单助手中的错误?

此代码有效

    <p>Some text.</p>
    <%= submit_tag "Submit", data: { disable_with: "Please wait..." }, :id => 'modal-submit', :class => "btn btn-primary" %>
    <% end %>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
    </div>
</div>

编辑:这个问题似乎不是由 Rails 引起的。发送到浏览器的 HTML 代码实际上是正确的。但是浏览器过早地关闭了表单。因此这一定是无效的 HTML,但我不知道为什么。

【问题讨论】:

    标签: html ruby-on-rails ruby-on-rails-4


    【解决方案1】:

    显然你不能在&lt;div&gt; 中启动一个表单而不关闭标签之前的表单。我通过将&lt;form&gt; 标签放在&lt;div class="modal-body"&gt; 之前解决了我的问题

    【讨论】:

    • 正确。有效的 HTML 的结构就像一棵带有标签节点的树 - 你不能做像 &lt;div&gt;&lt;form&gt;&lt;/div&gt;&lt;/form&gt; 这样的事情。
    【解决方案2】:

    看看表格从哪里开始,在哪里结束。这行得通吗?

    <div class="modal-dialog">
    <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times; </button>
      <h4 class="modal-title" id="myModalLabel">My form</h4>
    </div>
    
    <%= form_tag(some_path(@object), remote: true, id: "my-form") do %>
    
    <div class="modal-body">
      <ul class="errors has-error"></ul>
      <p>Some text</p>
      <table class="table table-striped show">
        <tr>
          <th></th>
          <th>Some value</th>
          <th>Some value2</th>
          <th>Some value3</th>
          <th>Some value4</th>
          <th>Some value5</th>
          <th>Some value6</th>
          <th>Some value7</th>
        </tr>
        <% @object.each do |o| %>
        <tr>
          <td><div class="checkbox"><label><%= check_box :condition1, o.value1 %></label></div></td>
          <td><%= o.value2.strftime("%d.%m.%Y") %></td>
          <td><%= o.value3 %></td>
          <td><%= o.value4 %></td>
          <td><%= number_with_precision o.value5, :precision => 2 %></td>
          <td><%= number_with_precision o.value6, :precision => 2 %></td>
          <td><%= number_with_precision o.value7, :precision => 2 %></td>
          <td><%= number_with_precision o.value8.strftime("%d.%m.%Y") %></td>
        </tr>
        <% end %>
      </table>
    
      <p>Some text.</p>
    
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
    <%= submit_tag "Submit", data: { disable_with: "Please wait..." }, :id => 'modal-submit', :class => "btn btn-primary" %>
    </div>
    
    <% end %>
    

    【讨论】:

    • 也不起作用。正如我在编辑中所指出的,生成的 HTML 看起来应该是这样。但是,它似乎是无效的 HTML。某些原因导致
      提前关闭。当我将生成的代码放入 HTML 验证器时,我收到错误“未打开的元素“FORM”的结束标记”
    猜你喜欢
    • 2021-11-21
    • 2012-10-20
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-22
    • 1970-01-01
    相关资源
    最近更新 更多