Ruby on Rails 3 Submit Form with a button
Submiting a form
<%= submit_tag %>
Is there anyway to submit aform using a button instead of the submit_tag? Nor do I want to use the image_submit_tag. What other options do I have to submit a form? I understand that the submit_tag produces <input type="submit">.
Thanks in advance.
Edit: I'm interested in reusing some of my css. I was thinking if it's possible to use this code below to submit the form. Bas开发者_StackOverflow社区ically I have css for a button but I want that button to do the submitting of the form. Or do I just rewrite the css for the submit_tag? Thanks for the help.
<%= link_to raw("<span>hello</span>"), root_path, :class=>"button grey"%>
Or is it more logical to just rewrite css for the submit tag?
adding a submit button means that pressing "enter" in a field also submits the form. Your other options are input type image, which is the same as submit, just with an image, or programmatically submitting your form with document.getElementById('form-id').submit();. You can hook that javascript on to pretty much anything.
To flip it around a bit, what are you trying to accomplish?
So you could attach document.getElementById('form-id').submit(); to onclick for your span and you are golden. The main issue at that point is that hitting enter in a text field will not submit your form (which may or may not be a big deal).
Ideally, you should style against semantic elements. So if you are making a button style, do it for button elements. It may be worth changing your style for that reason, but at the end of the day, a click handler for the span that submits your form will get the job done.
精彩评论