2012-03-05 2 views
0

단추로 사용되는 일부 이미지를 동적으로 삽입하고 onClick 이벤트를 추가하십시오.동적으로 onclick 처리기를 대체하십시오.

$("img.image_edit_customer").live("click", function() { 
    // when clicking the edit image of a row 
    $(this).manipulateCustomer("editRowCustomer", { 
     scope : "#customer_tbody" 
    }); 
}); 

나중에 버튼으로 사용되는 이미지에서 onClick 이벤트를 제거하고 싶습니다.

$('img[class^="image_edit"]').live('click', function() { 
    // do nothing from now on 
    alert('edit'); 
}); 

지금, 항상 이전 새로운 핸들러를 실행합니다.

UPDATE

내가 die('click');을 사용하고 있다면, 나는 아직 한 번 onclick 이벤트를 실행할 수 있습니다. 당신이 이벤트 핸들러 제거 .die() 방법을 사용해야하는 경우 바인딩 .live()을 사용하기 때문에

답변

1

한 번만 실행되는 이벤트를 부착하고, 다음 자체를 제거 : 첨부

을 제거하기 위해

$(document).off("click", "img.image_edit_customer", editRow); 

기능 : 이벤트를 제거하려면

$(document).on("click", "img.image_edit_customer", editRow); 

:

$(document).one("click", "img.image_edit_customer", function() { 
    $(this).manipulateCustomer("editRowCustomer", { 
     scope : "#customer_tbody" 
    }); 
}); 

나중에 제거 할 수있는 이벤트를 부착하려면

function editRow(e) { 
    $(e.target).manipulateCustomer("editRowCustomer", { 
     scope : "#customer_tbody" 
    }); 
} 
+0

이라고 언급하는 것이 좋습니다. 동적으로 삽입 된 이미지에도 적용됩니까? –

+0

예, 처리기를 연결할 수있는 모든 요소는이 메서드로 작동해야합니다. 그것은 어떤 'click'이벤트에 대한 문서 레벨을 기다립니다. 따라서 대상 요소의 selector가 올바르면 작동하지만, 'document'보다 더 가까운 부모를 사용해야합니다. 물론 그것은 물론입니다. 가능한 경우 역동적이지 못하다. – adeneo

0

:

$('img[class^="image_edit"]').die('click'); 

공지 jQuery를 1.7로

를, 사용하는 것이 좋습니다 대신 이벤트 처리기를 연결하거나 연결 해제하려면 .on().off() 메서드를 사용해야합니다. 라이브 방법으로 이벤트를 부착하고 있기 때문에

오히려 바인딩 해제 또는 (죽는 일명) 일치하는 모든 요소에 대한 이벤트를 죽이는 것보다

http://api.jquery.com/die/

2

, 당신은 다이 방법을 사용할 필요가 그래서 같은 '마커'클래스 개별 요소 : jQuery를 1.7+를 들어

$("img.image_edit_customer").live("click", function() { 
    if(!$(this).hasClass('clicked')) { 
     $(this).addClass('clicked'); 
     $(this).manipulateCustomer("editRowCustomer", { 
      scope : "#customer_tbody" 
     }); 
    } 
}); 
+1

+1 'live'와 'die'둘 다 사용되지 않습니다. hought. – fardjad

+0

이것을 –

0

, 나는 표시 줄

관련 문제