[removed] Form onSubmit does not work when function is called 'submit'

前端 未结 2 1948
别那么骄傲
别那么骄傲 2021-01-24 08:36

I am trying to call a JavaScript function when the submit button of a form is clicked.

For some reason, the function is not called when it is named \'submit\', but when

2条回答
  •  爱一瞬间的悲伤
    2021-01-24 08:51

    Because submit() is the reserved function to submit a form in core JS.

    For example, if your form had an id like myform, you could do this to submit it via JS:

    document.form['myform'].submit();
    

    So calling that function you're actually submitting the form and not calling your custom function.

    EDIT: I forgot to mention, it only works inside the form because it's calling the submit function of that form. Out of it it's not available anymore (since the body tag doesn't have a submit function).

提交回复
热议问题