使用javascript从表单中删除onsubmit值


Removing the onsubmit value from a form with javascript

我有一些onsubmit="return false;"的表单,例如:

<form id="form-nr-56" action="" method="post" onsubmit="return false;">
formstuff
</form>

如果满足某些条件,我将通过javascript 提交表格

$('#form-id').submit();

但我需要先从有问题的表单中删除onsubmit="return false;"。

如何做到这一点?

EDIT:我在onsubmit中没有使用javascript检查,因为表单验证使用了大量ajax请求和setTimeout,所以它总是返回false。

查看jquery.com文档:http://api.jquery.com/removeAttr/

这个答案是你的技术问题。这里的一些其他回复以更合乎逻辑的方式回答了这个问题,其他方式可能会更好地实现你想要做的事情。

<script>
    if(//your codition here)
    {
       $('#form-id').submit();
       return false;
    }
</script>
    <script>
    //write your conditions and return false if not valid other wise otherwise 
   function example()
   {
       if your condition false 
        return false;
       $('#form-id').submit();
   }  
    </script>

您不需要添加onSubmit = "return false;"

,你可以按以下方式操作

$('#form-id').submit(function(e)
{
    e.preventDefault();
    var isTrue = false;
    //conditions.
    //set isTrue = true on all conditions validation.
    //and then return isTrue.
    return isTrue;
});

您可以使用jQuery停止提交,然后添加您的逻辑。

$('#form-id').on('submit', function(event) {
    event.preventDefault();
});
$('#form').on('submit', function(e) 
{
    e.preventDefault();
    // Do what you want before the form submits
}

试试这个&检查