简体   繁体   English

Parsley.js验证未触发

[英]Parsley.js validation not triggering

I'm using Parsley.js to validate my form. 我正在使用Parsley.js来验证我的表单。 I tell Parsley to monitor the form by adding... 我告诉Parsley通过添加...来监视表单。

<form data-validate="parsley" name="myform"> 

And my elements look like: 我的元素看起来像:

<input type="text" name="username" data-required="true"/>

If you neglect the required fields and attempt to submit the form using this button... 如果您忽略必填字段并尝试使用此按钮提交表单...

<input type="submit" value="Submit"/> 

Parsley displays an error and does not allow the form to be submitted. Parsley显示错误,并且不允许提交表单。 However, if you use the following submit method instead... 但是,如果改为使用以下提交方法...

<a href="#" onClick="document.myform.submit();"> Submit </a> 

Parlsey simply ignores the errors and submits the form. Parlsey只是忽略错误并提交表单。 What is going on? 到底是怎么回事? Is Parsley listening specifically to the input[type=submit] element rather than the submission of the form itself? Parsley是否在专门侦听input [type = submit]元素,而不是表单本身的提交? (I need to make the "Submit" feature a link because I want to apply special styles to it.) (我需要使“提交”功能成为链接,因为我想对其应用特殊的样式。)

I'm not sure how parsley figures that it needs to validate, but you can always trigger validation yourself with document.myform.parsley( 'validate' ); 我不确定香菜如何计算需要验证的数量,但是您始终可以使用document.myform.parsley( 'validate' );来触发验证document.myform.parsley( 'validate' ); , so in your case you'd have to do: ,因此您必须执行以下操作:

if (document.myform.parsley( 'validate' ))  
     {document.myform.submit();}

I would expect Parsley to have a on submit handler automatically, but until I find out from the source you can do this: 我希望Parsley会自动有一个on Submit处理程序,但是直到我从源代码中找出来之前,您都可以这样做:

$('form#my-form').on "submit", (e)->
  $(this).parsley().validate()

Note that this is coffeescript. 请注意,这是coffeescript。 JS would be JS将

$('form#my-form').on("submit", function(e) {
  return $(this).parsley().validate();
});

Of course all your inputs have to have the data attributes set such as: 当然,所有输入都必须设置数据属性,例如:

data-parsley-required="true"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM