简体   繁体   English

单击提交按钮以进行引导模式和 parsley.js 验证后如何防止页面重新加载?

[英]How to prevent page reload after submit button clicked for bootstrap modal and parsley.js validation?

I have bootstrap modal which open up a modal to allow user to enter the form and it works.我有引导模式,它打开一个模式以允许用户输入表单并且它可以工作。 I implemented parseley.js for the form validaton and it kinda works.我为表单验证实现了 parseley.js,它有点工作。 The problem is when the validation passed, it reload the page (as you can see from below gif) which it not the behavior I expect.问题是当验证通过时,它会重新加载页面(从下面的 gif 中可以看到),这不是我期望的行为。 What I expect is to see the alert('ok') in validateForm() .我期望在validateForm()中看到alert('ok') ) 。

在此处输入图像描述

This is how I define my form tag and submit button这就是我定义表单标签和提交按钮的方式

<form id="myForm" data-parsley-validate="">
    <button class="btn btn-default" >Save</button>           
</form>

and this is my function to validate the form using Parsely.js.这是我的 function 使用 Parsely.js 验证表单。 When all validation pass, I expect is to see the alert('ok') but it doesn't.当所有验证通过时,我希望看到alert('ok')但它没有。 Instead, it reload the page.相反,它重新加载页面。

function validateForm() {
    $('#myForm').parsley().on('field:validated', function () {
        debugger
        var ok = $('.parsley-error').length === 0;
        $('.bs-callout-info').toggleClass('hidden', !ok);
        $('.bs-callout-warning').toggleClass('hidden', ok);
    })

        .on('form:submit', function () {
            debugger
            alert('ok') // I expect to reach here when pass validation pass but it doesn't
            postCustomer();
            return false; 
        });
}

and this is my bootstrap modal这是我的引导模式

$('#modalCustomerForm').on('shown.bs.modal', function () {
    debugger
    validateForm();
    return false;
})

If I add sample code below, there is no css class added by parsley.如果我在下面添加示例代码,则没有 css class 添加欧芹。 I suspect parsley is not loading.我怀疑欧芹没有加载。 But then, the validation work.但是,验证工作。 When I press Submit button, I can see red color error message at the bottom of the textbox.当我按下提交按钮时,我可以在文本框底部看到红色错误消息。

<div class="bs-callout bs-callout-warning hidden">
    <h4>Oh snap!</h4>
    <p>This form seems to be invalid :(</p>
</div>

<div class="bs-callout bs-callout-info hidden">
    <h4>Yay!</h4>
    <p>Everything seems to be ok :)</p>
</div>

Further troubleshooting.进一步的故障排除。 If I add an id to the button and create button click event, it doesn't auto reload the page.如果我向按钮添加 id 并创建按钮单击事件,它不会自动重新加载页面。 But if I do so, parsley.js validation doesn't work.但如果我这样做,则 parsley.js 验证不起作用。

<button class="btn btn-default" type="submit" id="formBtn">Save</button>

$('#formBtn').on("click", function (e) {
    debugger

    postCustomer();
    debugger
    e.preventDefault();
})

That's why I put my postCustomer() at here (below) in validateForm() but it doesn't work either:这就是为什么我将我的postCustomer()放在validateForm() ) 中(如下)的原因,但它也不起作用:

.on('form:submit', function () {
    
    debugger
    alert('ok')
    postCustomer();
    return false; // Don't submit form for this demo
});

Make sure you don't have nested form tags in your rendered code like this one below:确保在呈现的代码中没有嵌套表单标签,如下所示:

<form data-parsley-validate="">
    <form id="#myForm" data-parsley-validate="">
        <button class="btn btn-default">Save</button>
    </form> 
</form>

You can check this part once the page is rendered in the browser.在浏览器中呈现页面后,您可以检查此部分。 Otherwise, you don't have stopPropagation method in your code and it is possible that the parent form is submitted and the page is reloaded.否则,您的代码中没有stopPropagation方法,并且可能会提交父表单并重新加载页面。

The following statement in your code shoud stop form submitting, just remove other lines in your method for testing.您代码中的以下语句应停止表单提交,只需删除您方法中的其他行进行测试。

.on('form:submit', function (e) {
    console.log("form submitted after validation");
    return false;
}

So, in your code, it is possible you have some issues (exceptions) in the methods above return false .因此,在您的代码中,上述方法中可能存在一些问题(异常) return false As a hack, you can easily add a new event without changing your code.作为 hack,您可以轻松添加新事件而无需更改代码。

$('#myForm').submit(function (e)
{
    console.log("form submit called");
    e.preventDefault();
});

If return false doesn't help, this new event should.如果return false没有帮助,这个新事件应该。 It is called on the top of the form, after your validation.在您验证后,它会在表单顶部调用。

You can simply get the event from the form submission and stop it from going the default way which is reloading the page after the form submission.您可以简单地从表单提交中获取事件,并阻止它采用在表单提交后重新加载页面的默认方式。

.on('form:submit', function (e) {
e.preventDefault();
debugger
alert('ok')
postCustomer();
return false; // Don't submit form for this demo

}); });

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

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