[英]Preventing form submission after validation by parsley.js
I have used parsley.js many times and have literally copied the code from my last use of parsley. 我已经多次使用parsley.js并且从字面上复制了我上次使用欧芹的代码。
However, every time I submit the form the page refreshes. 但是,每次我提交表单时页面都会刷新。 preventDefault
seems to work on my other pages and stops the page from refreshing but for some reason when I tried now it won't work. preventDefault
似乎可以在我的其他页面上工作,并阻止页面刷新,但由于某些原因我现在尝试它将无法正常工作。 Can anyone figure out why not? 任何人都可以找出原因吗?
<script>
$(function(){
$("#register_signup").submit(function(e){
e.preventDefault();
var form = $(this);
if ($('#rform').parsley( 'isValid' )){
alert('valid');
}
});
});
</script>
<form id='rform' name='rform' data-parsley-validate>
<input id='reg_password' class='register_input' type='text' autocomplete="off" data-parsley-trigger="change" placeholder='Password' required>
<input id='reg_cpassword' class='register_input' type='text' name="reg_cpassword" placeholder='Confirm password' data-parsley-equalto="#reg_password" required>
<input id='register_signup' type="submit" onClick="javascript:$('#rform').parsley( 'validate' );" value='Sign Up' />
</form>
You are binding the submit
event to a input
element. 您将submit
事件绑定到input
元素。 If you check the jquery $.submit() documentation , it states that: 如果你检查jquery $ .submit()文档 ,它说明:
The submit event is sent to an element when the user is attempting to submit a form. 当用户尝试提交表单时,submit事件将发送到元素。 It can only be attached to
<form>
elements . 它只能附加到<form>
元素 。 Forms can be submitted either by clicking an explicit<input type="submit">
,<input type="image">
, or<button type="submit">
, or by pressing Enter when certain form elements have focus. 可以通过单击显式<input type="submit">
,<input type="image">
或<button type="submit">
,或者在某些表单元素具有焦点时按Enter键来<button type="submit">
表单。
This is your main problem and this is why alert
will never be displayed (in fact, that code is never executed). 这是您的主要问题,这就是永远不会显示alert
原因(事实上,该代码永远不会被执行)。
I would also change a few things: 我也会改变一些事情:
$('#rform').parsley( 'validate' )
should be $('#rform').parsley().validate()
, assuming you are using Parsley 2.* $('#rform').parsley( 'validate' )
应该是$('#rform').parsley().validate()
,假设你正在使用Parsley 2. * $('#rform').parsley( 'isValid' )
should be $('#rform').parsley().isValid()
. $('#rform').parsley( 'isValid' )
应该是$('#rform').parsley().isValid()
。 $.on()
instead of $.submit()
. 使用$.on()
而不是$.submit()
。 onClick
from the register_signup
element. 从register_signup
元素中删除onClick
。 Since you are already using javascript, I would do this directly in the javascript code instead of onclick. 由于您已经在使用javascript,我会直接在javascript代码而不是onclick中执行此操作。 This is more a personal preference. 这更像是个人偏好。 So, your code will be something like this: 所以,你的代码将是这样的:
<form id='rform' name='rform'>
<input id='reg_password' class='register_input' type='text' autocomplete="off"
data-parsley-trigger="change" placeholder='Password' required>
<input id='reg_cpassword' class='register_input' type='text' name="reg_cpassword"
placeholder='Confirm password' data-parsley-equalto="#reg_password" required>
<input id='register_signup' type="submit" value='Sign Up' />
</form>
<script>
$(document).ready(function() {
$("#rform").on('submit', function(e){
e.preventDefault();
var form = $(this);
form.parsley().validate();
if (form.parsley().isValid()){
alert('valid');
}
});
});
</script>
When you apply data-parsley-validate to your form, you don't need to apply javascript to form to stop submit until all validation run. 当您将data-parsley-validate应用于表单时,您不需要将javascript应用于表单以停止提交,直到所有验证运行。 But if you applying javascript return false when parsely() not valid. 但是如果你应用javascript,当parsely()无效时返回false。 And just make sure you have include parsley.js code file. 并确保您已包含parsley.js代码文件。
if you are using parsely 2 you can try this 如果您使用parsely 2,您可以试试这个
$(function () {
//parsely event to validate form -> form:valiate
$('#rform').parsley().on('form:validate', function (formInstance) {
//whenValid return promise if success enter then function if failed enter catch
var ok = formInstance.whenValid()
//on success validation
.then(function(){
alert('v');
formInstance.reset();
})
//on failure validation
.catch(function(){
formInstance.destroy();
});
$('.invalid-form-error-message')
.html(ok ? '' : 'You must correctly fill *at least one of these two blocks!')
.toggleClass('filled', !ok);
// console.log(formInstance);
if (!ok)
formInstance.validationResult = false;
console.log(formInstance);
});
//parsely event to submit form -> form:submit
$('#rform').parsley().on('form:submit', function (formInstance) {
// if you want to prevent submit in any condition after validation success -> return it false
return false;
});
//default submit still implemented but replaced with event form:submit
$('#rform').submit(function () {
alert('dd');
});
});
for more details parsely documentation check Form with examples and events 有关详细信息,请参阅文档检查表单以及示例和事件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.