How to suppress Click event on when clicking on the button on the ?

后端 未结 3 618
时光取名叫无心
时光取名叫无心 2021-01-15 00:36

I have a click event on the table row that executes an action. However when I click on the button on the same row, I would like to prevent the on-row-click event and simply

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-15 01:06

    You should remove the click behavior from the TR that that button resides. You can use a jQuery method like $(this).closest('tr') we can search up from the button through the DOM to find the TR, alternatively you could find the parent using parent functions in jQuery. Once we find the element (TR) we are looking for we can unbind the click function from it.

    So your final call would look like this:

    $(this).closest('tr').unbind('click');
    

    I've inserted this into your code and it works as expected, removing the click event from the TR

    $(document).ready(function() {
        $('#tab_open_deals tbody tr').on('click', function() {
            var row = $('');
            row.load('/echo/html/', {
            html: '
    Sub1Sub2
    12
    34
    ' }); $(this).find(".my_btn").on('click', function(){ $(this).closest('tr').unbind('click'); alert("clicked") }) $(this).find(".my_btn").show(); row.insertAfter($(this)); }); });

    I hope this works well for you.

提交回复
热议问题