简体   繁体   English

如何从单元格内的锚访问表行对象?

[英]how to access table row object from an anchor within a cell?

i have a table with a few rows. 我有几行的桌子。 i want to access the cells with the value that are in the same row as the anchor that triggers the function populateRow(); 我想访问与触发函数populateRow()的锚在同一行中的值的单元格; (i hope i explained it in a clear way). (我希望我能清楚地解释它)。 any idea how to do that? 知道怎么做吗?

<tr>
   <td>
         some value
   </td>
   <td>
       <a href="#myPopup" data-rel="popup" class="ui-btn ui-btn-inline ui-corner-all ui-icon-check ui-btn-icon-left" onclick="populateRow();">update</a>
   </td>
</tr>
<tr>
    <td>
          another value
    </td>
    <td>
        <a href="#myPopup" data-rel="popup" class="ui-btn ui-btn-inline ui-corner-all ui-icon-check ui-btn-icon-left" onclick="populateRow();">update</a>
   </td>
</tr>

The populateRow() function would need to know which anchor was clicked, so you'd need to either pass this to it to give it a reference to said anchor: populateRow()函数需要知道单击的锚,所以你需要或者通过this把它给它所述锚定器的参考:

onclick="populateRow(this);"

function populateRow(anchor) {
  var row = $(anchor).closest("tr");
  var firstCellValue = row.find("td").first().text();
}

...and from there use .closest() to navigate up the DOM to the containing tr element. ...然后从那里使用.closest()将DOM向上导航到包含tr的元素。

Or, better, bind the click event handler with jQuery rather than putting it inline on every row: 或者,最好将click事件处理程序与jQuery绑定,而不是将其内联到每一行中:

 $(document).ready(function() { $("#idOfYourTable").on("click", "a", function(e) { var row = $(this).closest("tr"); var firstCellValue = row.find("td").first().text(); console.log(firstCellValue); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table id="idOfYourTable"> <tr> <td>Value 1</td> <td> <a href="#myPopup" data-rel="popup" class="ui-btn ui-btn-inline ui-corner-all ui-icon-check ui-btn-icon-left">update</a> </td> </tr> <tr> <td>Value 2</td> <td> <a href="#myPopup" data-rel="popup" class="ui-btn ui-btn-inline ui-corner-all ui-icon-check ui-btn-icon-left">update</a> </td> </tr> </table> 

(Click the "Run..." button to see it working.) (单击“运行...”按钮以查看其工作。)

I don't know what is that populateRow() function for, but if you want to get value on the first td of the same tr , then you can try testing this: 我不知道populateRow()函数的作用是什么,但是如果您想在同一tr的第一个td上获取值,则可以尝试测试一下:

onclick="alert($(this).closest('tr').find('td:first').text())" 的onclick = “警报($(本).closest( 'TR')找到( 'TD:第一'。)文本())”

if you want to pass it in populateRow() function, you can: 如果要在populateRow()函数中传递它,则可以:

onclick="populateRow($(this).closest('tr').find('td:first').text())" 的onclick = “populateRow($(本).closest( 'TR')找到( 'TD:第一'。)文本())”

Or ... It would be more neat if you pass the element then process it later inside your function: 或者...如果您传递该元素,然后稍后在您的函数中对其进行处理,则会更加整洁:

onclick="populateRow(this)" 的onclick = “populateRow(这)”

function populateRow(elm) {
    var val = $(elm).closest('tr').find('td:first').text();
    alert(val);
}

Hope this helps. 希望这可以帮助。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM