Prevent form from submitting

允我心安 提交于 2019-12-12 10:22:32

问题


I have this

$("#contact-frm-2").bind("jqv.form.result", function(event , errorFound){
    if(!errorFound){
        alert('go!');
        event.preventDefault();
        return false;                               
    }
});

It supposed to validate the form for errors and when there is no error on the form, alert "Go!" without submitting. I am using the jquery.ValidationEngine.js you can find at http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/

I am sure

event.preventDefault();
            return false;

is supposed to prevent the form from submitting but it is still submitting. Can someone tell me what I am missing? Thanks


回答1:


The "onsubmit" JavaScript event will handle this for you.

jQuery:

$('#formid').submit(function(e) {
    e.preventDefault();
});

Pure JS:

document.getElementById('formid').addEventListener('submit', function(e) {
    e.preventDefault();
});



回答2:


$('#form_id').submit(function(e) {
    e.preventDefault();
    //...
});



回答3:


Yes, return false; should prevent your form from being submitted, but only if you execute it on the actual submit event. Your code above looks like it is dependent on the plugin. Why don't you try attaching an additional listener to the actual submit button in your form.

$("#submit_btn").on('click',function(){
  return false;
});



回答4:


The plugin has options that can be used. One of them is OnvalidationComplete which does the trick as shown below:

jQuery("#contact-frm-2").validationEngine('attach', {
   promptPosition : "centerRight", scroll: true,
   onValidationComplete: function(form, status){
   alert("The form status is: " +status+", it will never submit");
   }  
});

Full documentation is here http://posabsolute.github.com/jQuery-Validation-Engine/



来源:https://stackoverflow.com/questions/12461755/prevent-form-from-submitting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!