2013-03-29 6 views
1

을 유발하는 방법에 대한 정보를 핸들러에 전달하는 방법은 내가 비슷한 할 수 있지만, 작업 방법이 있는지 알고 싶어이jQuery를 트리거 - 이벤트

$(document).on('click','.someclass',function(){ 
    $('.otherclass').trigger('keyup',{aField:'my data'}); 
}); 

$(document).on('keyup','.otherclass',function(e){ 
    console.log(e.aField); // and to have printed 'my data' 
}); 

처럼 뭔가를 완. 왜냐하면 그것은 그렇게 작동하지 않습니다.


은 아마 예는 매우 명확하지 않습니다 : 내가 링크를 클릭하면, 나는 inputkeyup 이벤트를 트리거 할뿐만 아니라, 나는 그것을 발사 할 때는 객체 {aField:'my data'}를 전달하려는.


어쨌든 나 자신이 이미 가지고 있습니다. 솔루션은 핸들러에

$(document).on('keyup','.otherclass',function(e,object){ 
    console.log(object.aField); // this printed 'my data' 
}); 

답변

0

업데이트를 하나 개 더 매개 변수 object을 추가했다 : 왜 대신 뭔가를 트리거하기 위해 노력하는 두 인스턴스에 대한 공통의 핸들러를 사용할 수 없습니다.

function handleInputAction(data) { 
    console.log(data.aField) 
} 

$(document).on('click','.someclass',function(){ 
    handleInputAction({aField:'my data'}); 
}); 

$(document).on('keyup','.otherclass',function(e){ 
    handleInputAction(e.aField); 
}); 

$(this) 또는 이벤트 대상을 사용하십시오.

$(document).on('keyup','.otherclass',function(e){ 
    console.log($(this).attr('aField')); 
    // or 
    console.log(e.currentTarget.aField); 
    // or 
    console.log($(e.currentTarget).attr('aField')); 
}); 
+0

의 풀 문서를 통해 이벤트에 인수를 전달하는 올바른 방법 이벤트 대상이 "입력"하지만 내가 필요하다 대답

$(document).on('click','.someclass',function(){ $('.otherclass').trigger('keyup',{aField:'my data'}); }); $(document).on('keyup','.otherclass',function(e, obj){ console.log(obj.afield); // and to have printed 'my data' }); 

입니다 어떻게 든 이벤트에 수동으로 트리거 할 때 전달하는 객체 {aField : 'my data'}에 액세스합니다. – Constantin

+0

내 업데이트 된 답변보기 – ryan

+0

일반 처리기가 없어도 좋다고 생각합니다. – Constantin

관련 문제