简体   繁体   English

防止表单提交

[英]Prevent form from submitting

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!"它应该验证表单是否有错误,当表单上没有错误时,警告“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/我正在使用 jquery.ValidationEngine.js,你可以在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?有人可以告诉我我缺少什么吗?

The "onsubmit" JavaScript event will handle this for you. “onsubmit”JavaScript 事件将为您处理这个问题。

jQuery: jQuery:

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

Pure JS:纯JS:

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

Yes, return false;是的, return false; should prevent your form from being submitted, but only if you execute it on the actual submit event.应该防止您的表单被提交,但前提是您在实际submit事件上执行它。 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;
});

The plugin has options that can be used.该插件具有可以使用的选项。 One of them is OnvalidationComplete which does the trick as shown below:其中之一是 OnvalidationComplete,它执行如下所示的技巧:

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/完整文档在这里http://posabsolute.github.com/jQuery-Validation-Engine/

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

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