2013-07-10 26 views
1

所以當modalLink被點擊我想抓住,並從其他td保存小號jQuery如何定位這些兄弟姐妹的數據?

<tr date-user-id="1NJD3A" data-user-details="{...}" class=""> 
    <td class="table-request-btn incoming-icon"> 
     <button class="modalLink" href="#modalrequest">Request Intro</button> 
    </td> 
    <td class="data-name request-row"> 
     Joe Blow 
    </td> 
    <td class="data-career request-row"> 
     Administrator 
    </td> 
    <td class="data-company request-row"> 
     Acme 
    </td> 
</tr> 



我的功能,到目前爲止它拋出的這個tr而不是其他tr內「的數據值無法解決的類型錯誤

您將如何處理此問題?

var wireSearchTable = function() { 
    $('.modalLink').unbind('click').bind('click', function() { 
     var request_name = $(this).parents('tr').siblings.data('name'); 
     var request_title = $(this).siblings.data('career'); 
     var request_company = $(this).siblings.data('company'); 
      requestIntroModal.wireRequestIntroModal(details); 

      console.log('request_name = '+request_name); 
      console.log('request_title = '+request_title); 
      console.log('request_company = '+request_company); 
     }); 
    }; 
+2

你能展示一個生成的HTML樣本(而不是你用來創建HTML的模板)嗎? –

+1

點符號,喲 – JeffRegan

+0

更新了HTML @DavidThomas謝謝! –

回答

1

我個人建議:

$('button.modalLink').click(function (e) { 
    e.preventDefault(); 
    var row = $(this).closest('tr'); 

    // I have absolutely no idea what the following is meant to do, 
    // if anything, therefore it's not in the linked demo, 
    // and remains commented-out here. 
    // requestIntroModal.wireRequestIntroModal(details); 

    people = { 
     'name': row.find('td.data-name').text(), 
      'career': row.find('td.data-career').text(), 
      'company': row.find('td.data-company').text() 
    }; 
    console.log(people); 
}); 

JS Fiddle demo

參考文獻:

+0

啊甜美的感謝:D –

+1

你真的很受歡迎,我很高興得到了幫助! =) –

1

也許這就是你想要的?

var wireSearchTable = function() { 
    $('.modalLink').unbind('click').bind('click', function() { 
     var this_tr = $(this).closest('tr'), // grab the parent <tr> 
      request_name = this_tr.siblings('.some-class-name-or-id').data('name'), 
      request_title = this_tr.find('.data-career').data('career'), 
      request_company = this_tr.find('.data-company').data('company'); 

     requestIntroModal.wireRequestIntroModal(details); 

     console.log('request_name = '+request_name); 
     console.log('request_title = '+request_title); 
     console.log('request_company = '+request_company); 
    }); 
};