如果字段为空,则使用jquery ujs阻止表单提交

[英]Preventing a form submission with jquery ujs if the field is empty


I have a form that submits with data-remote => true.

我有一个提交data-remote => true的表单。

The form has one field, a textarea. I want to prevent submission of the form if the textarea length is 0 and only allow a form submission if the length is GT 1.

表单有一个字段,textarea。如果textarea长度为0,我想阻止提交表单,如果长度为GT 1,则只允许表单提交。

How do I bind to the form in a rails 3, jquery ujs friendly way. I tried the following but that did not stop the form submissions:

如何在rails 3中以jquery ujs友好的方式绑定到表单。我尝试了以下但是没有停止表单提交:

 $('#new_feedback').bind('ajax:before', function() {
      e.preventDefault();
 } );

Thanks

谢谢

1 个解决方案

#1


7  

You can bind directly to the form itself. This way, you can check the field you want as well as any other fields you may want to check when the form is submitted. return false is what cancels the form submission.

您可以直接绑定到表单本身。这样,您可以检查所需的字段以及提交表单时可能要检查的任何其他字段。 return false是取消表单提交的内容。

$('#myform').submit(function()
{
   if($('#fieldtocheck').val().length < 1)
   {
      return false;
   }
});

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2011/08/09/358d4f8f59e6a9d90af567fa5b3419c8.html



 
粤ICP备14056181号  © 2014-2019 ITdaan.com

赞助商广告