Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ajax表单提交前的验证_Javascript_Jquery_Ruby On Rails_Ajax - Fatal编程技术网

Javascript ajax表单提交前的验证

Javascript ajax表单提交前的验证,javascript,jquery,ruby-on-rails,ajax,Javascript,Jquery,Ruby On Rails,Ajax,如何在提交远程表单之前引入验证 我有一个名为validateForm()的javascript函数。我能够在AJAX请求过程之前调用它。我尝试使用return false和event.preventDefault。但似乎没有效果。下面是我的代码 <% remote_form_for :customer, :update =>"uxScreenLoaderDiv", :url => {:action => "login", :controller => "site"}

如何在提交远程表单之前引入验证

我有一个名为validateForm()的javascript函数。我能够在AJAX请求过程之前调用它。我尝试使用return falseevent.preventDefault。但似乎没有效果。下面是我的代码

<% remote_form_for :customer, :update =>"uxScreenLoaderDiv", :url => {:action => "login", :controller => "site"},  :onsubmit => "validateForm(event)" do |f| %>
    User name : <%= f.text_field "uxUserName", :class => "TextBox", :style => "width:100px;" %>&nbsp;*
    &nbsp;Password : <%= f.password_field "uxPassword", :class => "TextBox", :style => "width:100px;" %>&nbsp;*
    <%= f.submit "Go!", :class => "Button-Simple", :id => "uxSubmitButton" %>   
<% end %>

我认为您需要的是onSubmit=“return validateForm(this)”之类的内容。然后,如果验证返回false(因为验证失败),则表单不应提交。

尝试此操作

<% remote_form_for :customer, :update =>"uxScreenLoaderDiv", :url => {:action => "login", :controller => "site"}, :html => {:id => "uxLoginForm"}, :onsubmit => "return validateForm(event)" do |f| %>

应该编辑它

<% remote_form_for :customer, :update =>"uxScreenLoaderDiv", 
         :url => {:action => "login", :controller => "site"}, 
         :html => {:id => "uxLoginForm", :onsubmit => "return validateForm(event)"} 
         do |f|
 %>
“uxScreenLoaderDiv”,
:url=>{:action=>“login”,:controller=>“site”},
:html=>{:id=>“uxloginfo”,:onsubmit=>“returnvalidateForm(event)”}
多夫|
%>
重新编辑

为什么不使用:条件?像下面这样的

<% remote_form_for :customer, :update =>"uxScreenLoaderDiv", 
         :url => {:action => "login", :controller => "site"}, 
         :html => {:id => "uxLoginForm"},
         :condition=>"validateForm(event)!=false"
         do |f|
 %>
“uxScreenLoaderDiv”,
:url=>{:action=>“login”,:controller=>“site”},
:html=>{:id=>“uxloginfo”},
:条件=>“validateForm(事件)!=false”
多夫|
%>

您可能需要检查LIvevalidation插件(),您可以使用它来执行Ajax活动记录验证

同时,拥有非ajax验证(即使您使用ajax验证)也很好,因为ajax在javascript浏览器中不起作用

干杯,
sameera

我早些时候试过。。它有一个问题。。。如果返回值为true,则表单将直接提交,不再是AJAX请求。如果您只是尝试执行纯AJAX请求,则不需要提交按钮。你可以使用一个简单的“按钮”类型的输入,比如使用onClick处理程序。使用return validateForm(event)只会使它成为一个普通的表单,并使表单与ajax请求一起运行。请检查我的“再次编辑”并让我知道发生了什么。好提示:我已经在使用jquery.validate插件了;但必须手动复制我的验证。
 :onsubmit => "return validateForm(event)
<% remote_form_for :customer, :update =>"uxScreenLoaderDiv", 
         :url => {:action => "login", :controller => "site"}, 
         :html => {:id => "uxLoginForm", :onsubmit => "return validateForm(event)"} 
         do |f|
 %>
<% remote_form_for :customer, :update =>"uxScreenLoaderDiv", 
         :url => {:action => "login", :controller => "site"}, 
         :html => {:id => "uxLoginForm"},
         :condition=>"validateForm(event)!=false"
         do |f|
 %>