开发者

jQuery event handler not working

开发者 https://www.devze.com 2022-12-22 05:43 出处:网络
I can bind a jquery event to this element like: <script type=\"text/javascript\"> $(\'#new_key\').ready(function()开发者_如何学C {

I can bind a jquery event to this element like:

<script type="text/javascript">
$('#new_key').ready(function()开发者_如何学C {
 alert('Handler for .submit() called.');
 return false;
});

It works as expected

but if i do:

<script type="text/javascript">
$('#new_key').submit(function() {
  alert('Handler for .submit() called.');
  return false;
});

it dont work. does anybody knows why? what am i missing?


You need to do:

$(function() { //equal to $(document).ready() {
  $('#new_key').submit(function() {
    alert('Handler for .submit() called.');
    return false;
  });
});

The form may not be ready to be bound to when you're calling, so you need to wrap it to execute and rig up the handler when the document is ready.


$.ready(), if used, ought to be used on the document to indicate the DOM has been fully-loaded.

$(document).ready(function(){

  $("#new_key").submit(function(e){
   e.preventDefault();
   alert("Submit called!");
  });

});​

Online Demo: http://jsbin.com/ojaqo3/edit

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号