我有两个form_tag,其中包含一个单独的HTML文件中的日期选择器字段。内容几乎相同,只是time_start在第一个表单中显示,而time_end在第二个表单中显示。
<%= form_tag sample_path, method: :get do %>
<%= text_field_tag "time_start", nil, class: "datepicker" %>
<%= hidden_field_tag "time_end", nil %>
<%= hidden_field_tag "other_info", nil %>
...
<%= submit_tag "Submit"%>
<% end %>
...
<%= form_tag sample_path, method: :get do %>
<%= hidden_field_tag "time_start", nil %>
<%= text_field_tag "time_end", nil, class: "datepicker" %>
<%= hidden_field_tag "other_info", nil %>
...
<%= submit_tag "Submit" %>
<% end %>但是,当我在time_end中选择一个日期时,即使没有按下submit按钮,表单也会被提交。
我猜是因为HTML中生成的输入字段具有相同的id。
<form action="sample" accept-charset="UTF-8" method="get">
<input type="text" name="time_start" id="time_start" class="datepicker hasDatepicker">
<input type="hidden" name="time_end" id="time_end">
<input type="hidden" name="other_info" id="other_info">
...
<input type="submit" name="commit" value="Submit" data-disable-with="Submit">
</form>
...
<form action="sample" accept-charset="UTF-8" method="get">
<input type="hidden" name="time_start" id="time_start">
<input type="text" name="time_end" id="time_end" class="datepicker hasDatepicker">
<input type="hidden" name="other_info" id="other_info">
...
<input type="submit" name="commit" value="Submit" data-disable-with="Submit">
</form>我在form_tag中添加了index或namespace选项,但它似乎不适用于form_tag。
<%= form_tag sample_path, {index: 'group_01', method: :get} do %>
...
<%= form_tag sample_path, {namespace: 'group_02',method: :get} do %>
...有什么方法可以区分每个form_tag中的元素Is吗?
发布于 2020-03-25 16:49:10
您说得对,form_tag不允许您提到的方法。
在本例中,由于您使用的是hidden_field_tag,因此可以传递3个选项,如here in the documentation所述。
因此,您需要手动将id属性添加到每个字段,如下所示:
<%= hidden_field_tag "other_info", nil, time_start: "custom_id" %>但是,您所描述的问题与ID本身无关,而是与您在Javascript中使用的插件有关。
https://stackoverflow.com/questions/60845174
复制相似问题