2010-10-04 56 views
1

我需要为某些输入元素添加一个颜色选择器。 我正在使用jQuery - 在元素上触发一个函数

$(".element").colorPicker(){ ... } 

这很好用。 问题是,页面有一个AJAX形式,当提交时,将覆盖以前的形式(新的输入字段等)。之后,colorPicker停止工作。

那么我怎样才能将这个功能激发到新创建的输入呢?

回答

1

只需重新附加ajax回调中的调用,因为我不相信有可靠的事件,您可以使用它来.live.delegate它,而不会透露更多信息。

+0

所以我在'ajax'成功函数中再次添加''部分' – Alex 2010-10-04 01:22:19

+0

在'ajax'success'函数中,重新调用'.colorPicker'。 – 2010-10-04 01:23:25

0

我相信这可能工作:

$(".element").live('click focus', function() { 
    var $this = $(this); 
    if (!$this.data('hasColorPicker')) { 
     $this.colorPicker({ /* ... */ }).data('hasColorPicker', true); 
     $this.click(); // trigger the color picker - assuming it binds itself to the click event 
    } 
}); 
0

什么MEDER说的是好的,但也,如果你是通过复制现有创建新的元素,可以考虑使用$.clone(true)让你的副本,它将会延续现有的事件绑定也是如此。