Jquery`on`事件处理程序:事件委托和传递自定义数据

时间:2014-01-28 22:18:11

标签: jquery

我有一个通过ajax调用加载的表单,这意味着我需要使用event delegation来检测表单中的选择框何时被点击。

我的代码如下所示:

$( document ).on( "click", "form select", selectFunction );

但是,除了使用event delegation之外,我还希望能够将自定义event.data传递给on处理函数。

所以,我喜欢让我的代码片段如下所示......

$( document ).on( "click", "form select", { foo: 'bar' }, selectFunction );

...而我的处理函数就像这样。

function selectFunction( event ) {
    console.log(event.data.foo); // outputs 'bar'
}

不幸的是,event.data会返回undefined

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

http://jsfiddle.net/2mMbT/6/

这有效 -

$(document).on("click","#form-select", {foo: "bar"},SubmitForm);

function SubmitForm(event)
{ 
   alert(event.data.foo); 
}

我认为问题出在您正在使用的选择器中 - “表单选择”或完全不同的其他内容。

相关问题