2016-11-20 27 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

相關問題