最新消息: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 - onClick doesn't fire with only one input when pressing enter - Stack Overflow

matteradmin1PV0评论

I'm having trouble with events in Internet Explorer 7.

When I have a form with two or more input[type=text] and I press enter, the events occurs in this order:

  1. submit button (onClick)
  2. form (onSubmit)

Sample code:

<form onSubmit="{alert('form::onSubmit'); return false;}">
    <input type="text">
    <input type="text">
    <input type="submit" onClick="{alert('button::onClick');}">
</form>

If I have only one input[type=text] and I press enter the submit button onClick event doesn't fire. Sample code:

<form onSubmit="{alert('form::onSubmit'); return false;}">
    <input type="text">
    <input type="submit" onClick="{alert('button::onClick');}">
</form>

I'm having trouble with events in Internet Explorer 7.

When I have a form with two or more input[type=text] and I press enter, the events occurs in this order:

  1. submit button (onClick)
  2. form (onSubmit)

Sample code:

<form onSubmit="{alert('form::onSubmit'); return false;}">
    <input type="text">
    <input type="text">
    <input type="submit" onClick="{alert('button::onClick');}">
</form>

If I have only one input[type=text] and I press enter the submit button onClick event doesn't fire. Sample code:

<form onSubmit="{alert('form::onSubmit'); return false;}">
    <input type="text">
    <input type="submit" onClick="{alert('button::onClick');}">
</form>
Share Improve this question edited Jan 14, 2018 at 19:55 Rich Churcher 7,6643 gold badges39 silver badges61 bronze badges asked Aug 12, 2008 at 23:01 Daniel SilveiraDaniel Silveira 42.6k36 gold badges101 silver badges124 bronze badges 1
  • 1 This question addresses this issue: ENTER key on a FORM with a single Input Field, will automatically SUBMIT with GET – lawnsea Commented Jul 7, 2010 at 15:26
Add a ment  | 

6 Answers 6

Reset to default 5

The button's onclick should (I think) only fire if the button is actually clicked (or when the focus is on it and the user clicks enter), unless you've added logic to change that.

Is the addition of the extra textbox possibly changing the tab order of your elements (perhaps making the button the default control in that case)?

From the dawn of browsers, a single input field in a form would submit on enter with or without a submit button. This was to make it simpler for people to search a site from a single search field.

If you need to execute some javascript in a form, it is safer practice to use the onsubmit of the form (and return false to stop submission) rather than executing script in the onClick of the submit button. If you need to execute javascript from a button, use type="button" instead of type="submit" - hope this clarified what I meant

If you want code to run when the user presses enter, just use the onSubmit handler.

If you want code to run when the user presses the button, and not when the user presses enter, use a button other than type="submit".

Interestingly, if you click on the screen (remove the focus from the textbox) on second example with only one textbox, the event onClick fires... So it's not an expected behaviour since it only occurs when you have just one textbox and you have the focus on the textbox.
I'm afraid you've found a bug on the browser and you'll have to find a workaround, or avoid using the onClick event in that case.
I use the onSubmit event for validations because it's a "safer" event that is more likely to work on different browsers and situations.

Out of curiosity, are you using a DOCTYPE, and if so, which one? I'm not saying inpatabilities with the DOCTYPE are the issue, but quirks mode is something to rule out before trying anything else.

You might want to include a dummy hidden input element to recreate the situation where you had two input elements... that way, you'll get both of the events fired

<FORM onSubmit="{alert('form::onSubmit'); return false;}">
    <INPUT TYPE="text">
    <input type="hidden" name="dummy">
    <INPUT TYPE="submit" onClick="{alert('buttom::onClick');}">
</FORM>

IE has a lot of confusing fixes that needs to be done for improving patibility of our code

Post a comment

comment list (0)

  1. No comments so far