简体   繁体   English

Javascript覆盖表单onsubmit事件无效

[英]Javascript override form onsubmit event not working

I am overriding a form submit event like this: 我正在覆盖这样的表单提交事件:

form.onsubmit = function(event) {
  event.preventDefault();

But when I call submit on the form like this in a separate function: 但是当我在一个单独的函数中调用这样的表单时提交:

form.submit();

The onsubmit function is not called and the and the form is posted as usual. 未调用onsubmit函数,并且像往常一样发布和表单。

Any thoughts? 有什么想法吗?

Edit: 编辑:

I am also creating a file input in this form and calling its click event immediately. 我也在这个表单中创建一个文件输入并立即调用它的click事件。 Would this affect the form submit event?: 这会影响表单提交事件吗?:

if (fileInput && document.createEvent)
{
    var evt = document.createEvent('MouseEvents');
    evt.initEvent('click', true, false);
    fileInput.dispatchEvent(evt);
}

Edit #2: 编辑#2:

I am submitting the form by calling the form's submit function after the file input value has changed: 我在文件输入值发生变化后通过调用表单的提交函数来提交表单:

function nameFileLabel(id)
{
    var f = document.getElementById('fileForm' + id);
    var l = document.getElementById("fileText_" + id);
    var i = document.getElementById("fInputId" + id);
    if (i.value != '')
    {
        l.innerHTML = i.value.replace('fakepath', '...');
        var theUploadForm = document.getElementById('fileDiv_' + id);
        theUploadForm.style.visibility = 'visible';
        theUploadForm.style.display = 'block';
        f.submit();
    }
}

To prevent the form posting your function must return false. 要防止表单发布,您的函数必须返回false。

form.onsubmit = function(event) {
    event.preventDefault();
    return false;
}

Try with this one 试试这个

form.submit = function() {
  //event.preventDefault();     No need of this call
  //do your processing here
}

As Manish pointed out, both overriding the submit function and calling the submit form in javascript was complicating how the event was propagating. 正如Manish所指出的,覆盖提交函数和在javascript中调用提交表单都会使事件的传播变得复杂。 So added a hidden button in to the form in javascript and called the button's click function instead of the form submit function. 因此,在javascript中向表单添加了一个隐藏按钮,并调用了按钮的单击功能,而不是表单提交功能。 WHich seems to have worked even it it feels rather like a kludge! 它似乎已经起作用了,它感觉就像一个kludge! Many thanks for to all of you for your prompt help. 非常感谢大家的快速帮助。 :-) :-)

function nameFileLabel(id)
{
    var f = document.getElementById('fileForm' + id);
    var l = document.getElementById('fileText_' + id);
    var i = document.getElementById('fInputId' + id);
    var b = document.getElementById('sub' + id);
    if (i.value != '')
    {
        l.innerHTML = i.value.replace('fakepath', '...');
        var theUploadForm = document.getElementById('fileDiv_' + id);
        theUploadForm.style.visibility = 'visible';
        theUploadForm.style.display = 'block';
        b.click();
    }
}

Following on @garryp's answer on "return false". 关于@ garryp关于“return false”的回答。 To add on, further reading from MDN event.stopImmediatePropagation to see differences and use case between event.preventDefault and event.stopPropagation. 要添加,请继续阅读MDN event.stopImmediatePropagation以查看event.preventDefault和event.stopPropagation之间的差异和用例。

On the side note, "return false;" 在旁注,“返回假”; is the same calling both; 是同样的呼唤两者;

  event.preventDefault();
  event.stopPropagation();

Hope this helps. 希望这可以帮助。

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

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