【发布时间】:2010-08-12 04:03:21
【问题描述】:
我有一个像
这样的 ajax 邮件表单 - form_remote_tag :url=>mails_path,:condition=>"validate_mail()", :html => {:id=>"mailform",:method => :post, :class => 'ajax',:style=>"padding:15px;" } do |form|
.gimmespace
Naam
%br
= text_field_tag :name,params[:name],:class=>"title required"
.gimmespace
Telefoonnummber
%br
= text_field_tag :phone,params[:phone],:size=>25,:class=>"title"
.gimmespace
Mailadres
%br
= text_field_tag :email,params[:email],:size=>30,:class=>"title required"
.gimmespace
Onderwerp
%br
= text_field_tag :subject,params[:subject],:class=>"title required"
.gimmespace
Boodschap
%br
= text_area_tag :message,params[:message],:rows=>10,:cols=>45,:class=>"title required"
.gimmespace
= submit_tag "Verstuur",:id=>"mailsubmit",:class=>"sendBtn"
%button{:onclick=>"$.fn.colorbox.close();"} Annuleer
以上代码在 HAML 中。它使 ajax 表单提交给控制器。我必须在提交之前验证这些字段。所以,我尝试了几个东西。我阅读了这篇文章http://hillemania.wordpress.com/2006/09/18/rails-ajax-pre-submit-form-validation/ 并对测试javascript函数进行了回调以进行验证。这是javascript验证函数。
function validate_mail() {
alert("Your Name, Email, Subject and Body Content are Required !");
return false;
}
根据上面的函数,它以任何方式返回 false,并且表单不应该被提交,但是,它以 ajaxically 的方式提交。有没有其他办法,请帮忙。
【问题讨论】:
标签: javascript ruby-on-rails ajax haml