Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Example</title>
</head>
<body>
<form action="http://google.com/search">
  <input type="text" name="q" value="kittens">
  <input type="submit" value="Send">
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
(function() {
  var counter = 0;
  
  // Hook a handler to the form's submit event. Note that KO will
  // use jQuery to do this if jQuery is on the page, so we'll do it
  // via jQuery
  $("form").on("submit", function(e) {
    var form = this;
    
    // Note that we got the event
    ++counter;
    display("submit event received, counter = " + counter);
    
    // Simulate asynchronous task
    setTimeout(function() {
      // We limit the number of times we'll re-fire
      if (counter < 3) {
        alert("submitting form using jQuery's submit function (counter = " + counter + ")");
        $(form).submit();
      } else {
        alert("but now we'll submit it directly, not through jQuery");
        form.submit();
      }
    }, 10);
    // By default, KO will prevent the default, so do that
    e.preventDefault();
  });
  
  function display(msg) {
    $("<p>").html(String(msg)).appendTo(document.body);
  }
})();
</script>
</body>
</html>
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
tjcrowderpro
0viewers