What is the difference between <input type = "submit"> and form.submit() in js?

What is the difference between <input type = "submit"> and form.submit() in js?

 

 

 

 

Sort by vote Sort by time

 

Thank you.
Those who say "the same" are not professional front-ends, at least the following differences:
Suppose:
A form has <input type="submit">, and the form is submitted by clicking on this input
B form does not have <input type="submit" >, but wrote an <input type="button">, and bound the click event on this input, and submitted it with js's form.submit() in the event listener.

1. In an input type=text in form A, the user is interacting with it (it is the current focus), the user only needs to press the enter of the keyboard to submit the form; while B does not work in some cases (partly For the situation refer to Enter to submit the form ), you can only click the button below; of course, you can bind a keypress event to each input, and then detect whether the pressed key is enter, if it is, submit it...as long as you don't feel pain .
2.B form submission will not trigger the onsubmit event of the form; A will trigger it.

In addition, there is also the difference between HTML semantics. The processing of form A is more semantic. As for A can submit the form when js fails to load (at least it can still be used), B can't and so on, I will not say.

In short, A, <input type="submit"> is better, B is a bit worse in user experience.

 
 

 

 

 

 

The same, but personally prefer to use js for processing. . After all, the era of HTML5 is coming. In order to make the revision easier in the future, try to avoid using the html tags of html4 to submit these things. js is eternal and will not be eliminated~

 

Both of you upstairs have said very well!
If you ask them about the background processing process after submitting, it is the same traditional synchronous request,
but now many websites are pursuing personalization and now ajax asynchronous submission, replacing button (submit) with beautified a tag. In this case, we Only use the onsubmit() function instead!
Because of our work habits, we generally don't use submit;
for the situation mentioned above, I think if you need to, you can add a submit button with a transparent layer and throw it in the form where it doesn't get in the way.

 
 

 

 

 

 

Whether to trigger onsubmit;
some browser security restrictions will cause js to actively trigger submit failure under certain circumstances

 

 

 

the same

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326972730&siteId=291194637