2009-10-24 64 views
1

HTML:的jQuery绑定事件

<ul> 
    <li><input type="submit" id="myId" value="someVal"/> 
</ul> 

jQuery的

$('ul').find('input[type="submit"]').click(function{ 
     alert('nasty alert') 
     $(this).attr('id','newId'); 
}); 

$('input#newId').click(function(){ 
      $(this).hide(); 
}); 

好了,我的本意是在一次点击,然后按钮做别的事情(隐藏)更改ID。我也尝试过live()。在萤火虫它看起来像id已经改变,但我第二次点击按钮触发相同的警报('讨厌警报')。和一些奇怪的...如果我使用live(),鼠标右键点击按钮消失(像它应该)。任何sugestions?感谢

回答

2

你基本上附加click事件处理两次以相同的输入。

没有理由你应该连接两个事件处理程序,我已经更新了代码,使一个变量被用于跟踪。

编辑:固定的语法错误,现在它的使用.data

<ul> 
<form> 
<input type=submit value=go> 
</form> 
</ul> 
<script src=http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js></script> 
<script> 


    $('ul').find('input[type="submit"]').click(function() { 
      if (!$(this).data('_clicked')) { 
       alert('nasty alert') 
       $(this).attr('id','newId'); 
       $(this).data('_clicked', true); 
      } else { 
       $(this).hide(); 
      } 
      return false; 
    }); 
</script> 
+0

嗯,理论上应该工作,但它没有,现在只是触发隐藏()方法 – kmunky 2009-10-24 01:20:57

+0

你有一个演示? – 2009-10-24 01:22:48

+0

不,我的代码是非常非常复杂,但我简化它得到的想法 – kmunky 2009-10-24 01:24:05