Ruby-on-rails 带有javascript函数的submit_tag
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7510260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
submit_tag with javascript function
提问by karthi_ms
I am new to rails.I want to call a javascript function when click the submit button.I used submit_tag , but the function did not get triggered.I want some thing like the following ,
我是 Rails 的新手。我想在单击提交按钮时调用一个 javascript 函数。我使用了 submit_tag ,但该函数没有被触发。我想要一些类似以下的东西,
<%= submit_tag , :onsubmit => "return validateform()" %>
I googled the problem but I couldn't find a solution.Please any one give a solution.
我用谷歌搜索了这个问题,但我找不到解决方案。请任何人给出解决方案。
回答by Mahesh
try onclick
尝试点击
<%= submit_tag , :onclick => "return validateform();" %>
or if you want to use onsubmit you can use that in your form_tag.
或者如果你想使用 onsubmit 你可以在你的 form_tag 中使用它。
回答by nateleavitt
You can use something like this on the form tag
你可以在表单标签上使用这样的东西
<% form_for :bla, :html => { :onsubmit => 'blahblah;' } do |f| %>
回答by Salil
You can try
你可以试试
<%= submit_tag , :onclick => "return validateform()" %>
OR
或者
You can Use button_to_function
您可以使用button_to_function
<%= button_to_function "Greeting", "validateform()" %>
And in your Javascript function, if all the validations pass, submit the form.
在您的 Javascript 函数中,如果所有验证都通过,则提交表单。
回答by Michael Frederick
You would do it in the form_tag...
你会在 form_tag 中做到这一点...
<%= form_tag({:action => :whatever}, {:onSubmit => 'return validateform();'} ) %>
<%= form_tag({:action => :whatever}, {:onSubmit => 'return validateform();'} ) %>

