Possible Bug in .submit()

Possible Bug in .submit()

I have a form for which i want to control submission, limiting it to when the submit button is clicked. However, if I attach a click event to the submit button, the event is triggered even when the button is not clicked. Here's a short example based on the .submit() docs page example.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <style>

  5.   p { margin:0; color:blue; }
  6.   div,p { margin-left:10px; }
  7.   span { color:red; }
  8.   </style>
  9.   <script src="http://code.jquery.com/jquery-latest.js"></script>
  10. </head>
  11. <body>
  12.   <p>Type 'correct' to validate.</p>
  13.   <form action="javascript:alert('success!');">
  14.     <div>
  15.       <input type="text" />

  16.       <input type="submit" />
  17.     </div>
  18.   </form>
  19.   <span></span>
  20. <script>

  21.     $("form").submit(function() {
  22.       if ($("input:first").val() == "correct") {
  23.         $("span").text("Validated...").show();
  24.         return true;
  25.       }
  26.       $("span").text("Not valid!").show().fadeOut(1000);
  27.       return false;
  28.     });

  29.     $("input:submit").click(function(){
  30.       alert('clicked');
  31.     });
  32. </script>
  33. </body>
  34. </html>
With this code, clicking into the input field and then pressing the enter key triggers the click event even though you've gone nowhere near the button to which it's attached.

This feels like a bug to me, but I'm open to being told that I've misunderstood how jQuery works is that's the case.

I'd also be grateful for any advice as to how I can differentiate between a real click and the exact type of entry that I'm trying to prevent :(
    • Topic Participants

    • peter