最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery trigger table row click event - Stack Overflow

programmeradmin0浏览0评论

Sample Code:

<table class="grid">
    <tr>
        <td><a href="#" id="unit_floor_plan_preview">click me &nbsp;</a></td>
        <td class="unitNumber"><span>Unit 1</span></td>
        <td class="unitStatus">Open</td>
    </tr><tr>
        <td class="unitNumber"><span>Unit 2</span></td>
        <td class="unitStatus">Sold</td>
    </tr>
</table>

I am able to get the row index of the row selected to get the exact column data.

tr = $('.grid').find('tr');

tr.bind('click', function(event) {

    unitNo = $(this).find("td.unitNumber span").html(); 
    alert(unitNo);


});

The above tr click event is fine.

My problem now is how to trigger this tr binding event when clicking the anchor link <td><a href="#" id="unit_floor_plan_preview">Show map &nbsp;</a></td>within the table row?

Goal: To get first the unitNo (processed on tr.bind click event) before processing the rest of the code on anchored link?

I tried to duplicate the tr click function within the anchor link click event but got undefined value on unitNo. See my code:

$('a[id^="unit_floor_plan_preview"]').bind('click',function() {
    var tr = $('.grid').find('tr');
    unitNo = $(this).find("td.unitNumber span").html();  

    alert('From link: ' + unitNo);

});

test code: /

Sample Code:

<table class="grid">
    <tr>
        <td><a href="#" id="unit_floor_plan_preview">click me &nbsp;</a></td>
        <td class="unitNumber"><span>Unit 1</span></td>
        <td class="unitStatus">Open</td>
    </tr><tr>
        <td class="unitNumber"><span>Unit 2</span></td>
        <td class="unitStatus">Sold</td>
    </tr>
</table>

I am able to get the row index of the row selected to get the exact column data.

tr = $('.grid').find('tr');

tr.bind('click', function(event) {

    unitNo = $(this).find("td.unitNumber span").html(); 
    alert(unitNo);


});

The above tr click event is fine.

My problem now is how to trigger this tr binding event when clicking the anchor link <td><a href="#" id="unit_floor_plan_preview">Show map &nbsp;</a></td>within the table row?

Goal: To get first the unitNo (processed on tr.bind click event) before processing the rest of the code on anchored link?

I tried to duplicate the tr click function within the anchor link click event but got undefined value on unitNo. See my code:

$('a[id^="unit_floor_plan_preview"]').bind('click',function() {
    var tr = $('.grid').find('tr');
    unitNo = $(this).find("td.unitNumber span").html();  

    alert('From link: ' + unitNo);

});

test code: http://jsfiddle/VjkML/29/

Share Improve this question asked Jun 18, 2013 at 17:36 BoydBoyd 491 gold badge1 silver badge8 bronze badges 1
  • The problem here is order of operations from event bubbling. The link will always e before the row click. – epascarello Commented Jun 18, 2013 at 17:52
Add a ment  | 

3 Answers 3

Reset to default 4

Change:

unitNo = tr.find("td.unitNumber span").html(); 

To:

unitNo = $(this).find("td.unitNumber span").html(); 

In $('a[id^="unit_floor_plan_preview"]').bind('click' You try to find "td.unitNumber span" within $(this). The problem is, this refers to the link clicked on, thus you'll never find anything!


FYI, you could easily rewrite that ENTIRE statement as follows:

$(document).on("click", '.grid tr, a[id^="unit_floor_plan_preview"]', function(e) {
    e.stopPropagation(); // will prevent double click events from link being clicked within row
    var unitNo = $.trim($(this).closest("tr").find(".unitNumber span").text()); // trim to remove end space, closest gets closest parent of selected type
    if (e.target.tagName == "A") alert('From link: ' + unitNo);
    else alert(unitNo);
});

Example

The best way is to bind the event only to your tr and use the event.target to check if it is tr or any other element that was clicked. This way your click event will not be duplicated.

This should work

var unitNo;
var tr;

tr = $('.grid').find('tr');
tr.bind('click', function (event) {

    if ($(event.target).is('tr')) {
        // If it is a tr that is clicked then just use find
        unitNo = $(this).find("td.unitNumber span").html();
    } else {
        // If not tr find the closest tr and then find the element
        unitNo = $(this).closest('tr').find("td.unitNumber span").html();
    }
    alert(unitNo);
});

Check Fiddle

Try:

var unitNo;
var tr = $('.grid').find('tr');
$('a[id^="unit_floor_plan_preview"]').on('click',function(e) {
    e.stopPropagation();
    unitNo = $(this).closest('tr').find("td.unitNumber span").html();  
    alert('From link: ' + unitNo);
});
tr.on('click', function(event) {
    unitNo = $(this).find("td.unitNumber span").html(); 
    alert(unitNo);
});

jsFiddle example

With the link you need to go up the DOM to the row (via .closest()), then use .find() to go back down the same row to find the span you want to get the Unit value.

发布评论

评论列表(0)

  1. 暂无评论