最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Event firing sequence for a submit button - Stack Overflow

matteradmin4PV0评论

Say a submit button has a click event handler as well (yes, it's wrong, but that's really the code I'm working on).

And the click handler sets a value in a field of the form that's about to be submitted.

So when the submit button is clicked:

  1. What will fire first, the form submit event or the click event?
  2. Will the value set in the form field by the click event submitted to the server? I think this depends on the answer to Q1. If the form is posted first, I'm guessing it won't.

Note: I'm not looking for alternatives and advice. I know what the proper way of handling this will be. Just trying to understand the sequence of events and their implications.

Say a submit button has a click event handler as well (yes, it's wrong, but that's really the code I'm working on).

And the click handler sets a value in a field of the form that's about to be submitted.

So when the submit button is clicked:

  1. What will fire first, the form submit event or the click event?
  2. Will the value set in the form field by the click event submitted to the server? I think this depends on the answer to Q1. If the form is posted first, I'm guessing it won't.

Note: I'm not looking for alternatives and advice. I know what the proper way of handling this will be. Just trying to understand the sequence of events and their implications.

Share Improve this question edited Jan 2, 2023 at 15:40 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 10, 2010 at 10:09 JSTyroJSTyro 611 silver badge2 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5
  1. The click event on the submit button
  2. Yes, events resolve before default functionality, so the event runs and then the button submits the form (firing the submit event and then actually submitting the data).

First will be called event, there You have last chance f.ex to block form submitting. Then submit with changed values by event routine.

Post a comment

comment list (0)

  1. No comments so far