Skip to content

Instantly share code, notes, and snippets.

@yuanchuan
Created May 22, 2011 17:41
Show Gist options
  • Save yuanchuan/985699 to your computer and use it in GitHub Desktop.
Save yuanchuan/985699 to your computer and use it in GitHub Desktop.
jQuery live('submit') and disabled submit button
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>jQuery live('submit') and disabled submit button </title>
<style>
form {margin-bottom:50px;}
</style>
</head>
<body>
<form method="post" action="">
<fieldset>
<legend>只有一个文本框,点击提交不会提交,但按下 enter 键会提交</legend>
<p><input type="text" /></p>
<p><input type="submit" value="提交" /></p>
</fieldset>
</form>
<form method="post" action="">
<fieldset>
<legend>多于一个文本框,点击或按enter键均不会提交</legend>
<p><input type="text" /></p>
<p><input type="text" /></p>
<p><input type="submit" value="提交" /></p>
</fieldset>
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script>
$(function(){
/**
* Got problem in IE<9
*/
$('form').live('submit', function(){
$(this).find('input[type="submit"]').attr('disabled', true);
});
/**
* But both of the two bellow will work fine:
*
* $('form').submit(function(){
* $(this).find('input[type="submit"]').attr('disabled', true);
* });
*
* $('form').live('submit', function(){
* $(this).find('input[type="submit"]').attr('title', 'click me');
* });
*/
});
</script>
</body>
</html>
@yuanchuan
Copy link
Author

Trick to make it work

$('form').live('submit',  function() {
  return !!(function(elem) {
    return setTimeout(function(){elem.find('input[type="submit"]').attr('disabled',  true)}, 0);
  })( $(this) );
});

$('input[type="submit"]').live('click',  function() {
  return !!(function(elem) {
    return setTimeout(function(){elem.attr('disabled',  true)}, 0);
  })( $(this) );
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment