在'提交'中自定义查询字符串参数凤凰城HTML表单中的按钮

时间:2016-11-20 00:20:45

标签: elixir phoenix-framework

当用户点击“提交”时,我想在请求中添加一些查询字符串参数。在HTML表单上。当前模板文件代码:

  <body>
  <h2>Create a new email subscription</h2>
  <p>This email address will recieve a message when a new order is placed.</p>
  <%= form_for @changeset, subscription_path(@conn, :create), fn f -> %>
    <label>
      Email Address: <%= email_input f, :email_address %>
    </label>

    <%= submit "Submit" %>
  <% end %>
</body>

似乎submit意味着支持某些opts,但是,它们没有文档记录。

https://hexdocs.pm/phoenix_html/Phoenix.HTML.Form.html#submit/2

当用户点击“提交”时,我是否可以将其他参数传递给请求?

1 个答案:

答案 0 :(得分:2)

如果您在"secure-http": false处查看submit的代码,则只需将选项传递给名为content_tag的其他函数

  def submit(_, opts \\ [])
  def submit(opts, [do: _] = block_option) do
    opts = Keyword.put_new(opts, :type, "submit")

    content_tag(:button, opts, block_option)
  end

  def submit(value, opts) do
    opts = Keyword.put_new(opts, :type, "submit")

    content_tag(:button, value, opts)
  end

如果您查看content_tag的文档 https://github.com/phoenixframework/phoenix_html/blob/v2.8.0/lib/phoenix_html/form.ex#L533您会看到一些可以通过的选项:

  

创建具有给定名称,内容和属性的HTML标记。

iex> content_tag(:p, "Hello")
{:safe, [60, "p", "", 62, "Hello", 60, 47, "p", 62]}
iex> content_tag(:p, "<Hello>", class: "test")
{:safe, [60, "p", " class=\"test\"", 62, "&lt;Hello&gt;", 60, 47, "p", 62]}

iex> content_tag :p, class: "test" do
...>   "Hello"
...> end
{:safe, [60, "p", " class=\"test\"", 62, "Hello", 60, 47, "p", 62]}

如果您需要向服务器发送其他数据,可以使用hidden_input https://hexdocs.pm/phoenix_html/Phoenix.HTML.Tag.html#content_tag/2