2016-11-20 26 views
1

我想在用户在HTML表单上点击'submit'时向请求添加一些查询字符串参数。当前模板文件中的代码:在Phoenix HTML表单的'Submit'按钮中自定义查询字符串参数

<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

有没有一种方法,我可以通过其他参数,当用户点击“提交”请求?

+0

你想总是传递额外的参数或点击提交按钮(即不是当只有当用户按下输入字段或使用任何其他方式提交表单)? – Dogbert

+0

无论用户提交表单的方式如何,我都需要这些参数。也许我应该使用'hidden_​​input' – sheldonkreger

+0

是的,'hidden_​​input'是要走的路。 – Dogbert

回答

2

如果你看看submit代码在https://github.com/phoenixframework/phoenix_html/blob/v2.8.0/lib/phoenix_html/form.ex#L533引擎盖下,它只是通过选项来命名的其他功能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://hexdocs.pm/phoenix_html/Phoenix.HTML.Tag.html#content_tag/2你会看到一些您可以通过的选项:

创建具有给定名称,内容和属性的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_inputhttps://hexdocs.pm/phoenix_html/Phoenix.HTML.Form.html#hidden_input/3

相关问题