简体   繁体   English

用户提交表单之前如何访问text_field的内容

[英]How to access the contents of a text_field before user submits the form

I have a form with a single text_field_tag . 我有一个带有单个text_field_tag的表单。 This form appears all over, living inside the application layout. 这种形式出现在整个应用程序布局中。 I use params[:controller] to set the url_for_options. 我使用params[:controller]设置url_for_options。 Depending on which page the user uses this text_field_tag on, the function will differ. 根据用户在哪个页面上使用此text_field_tag ,功能会有所不同。

The logic is then held inside the create action of the numerous controllers that use this search form. 然后,将逻辑保存在使用此搜索表单的众多控制器的create动作中。

If the page that the user searches on isn't one of the specified controllers I have preset, it currently defaults to one. 如果用户搜索的页面不是我预设的指定控制器之一,则当前默认为一个。 I want to be able to change the default controller used depending on the contents of the text_field_tag . 我希望能够根据text_field_tag的内容更改使用的默认控制器。

So if the user enters an IP address, I will route them to page x. 因此,如果用户输入IP地址,我会将其路由到页面x。 If the user enters an email address, I will route them to page y. 如果用户输入电子邮件地址,我会将其路由到页面y。

My question is, how can I check to see what the user has entered either before they hit submit, or after they hit submit, but before I set the url_for_options? 我的问题是,如何在用户单击“提交”之前或之后,但在设置url_for_options之前检查用户输入的内容?

I have a an if/elsif/else statement handling the url_for_options inside my form partial. 我有一个if / elsif / else语句,用于处理表单部分中的url_for_options。 Inside the else statement, I set a default search route which sets the url_for_options. 在else语句中,我设置了一个默认的搜索路由,该路由设置了url_for_options。 I'd basically like a way to, inside the else block, check if the text_field_tag is a an IP versus an email address (I can do the actual checking with regex, and don't need help with that). 我基本上想要一种方法,可以在else块中检查text_field_tag是否是IP地址而不是电子邮件地址(我可以使用regex进行实际检查,并且不需要帮助)。 But my problem is that I can't find a way to see inside the text_field_tag until I've already submitted the form, and I'm not sure where I can put the logic to check the insides of the text_field_tag and then redirect them to the desired page. 但是我的问题是,在提交表单之前,我找不到在text_field_tag内部查看的方法,而且我不确定在哪里可以放置逻辑以检查text_field_tag的内部,然后将其重定向到所需的页面。

You can specify onsubmit in the form tag to run a custom JS function for this. 您可以在form标记中指定onsubmit来为此运行定制的JS函数。

IE IE浏览器

<form onsubmit="setURL()" ...>

When the user submits the form, it will call the setURL() function first. 当用户提交表单时,它将首先调用setURL()函数。 From there you could use JS or jQuery to set the action to a specific URL or page based on the input value. 从那里,您可以使用JS或jQuery根据输入值将操作设置为特定的URL或页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM