2017-04-02 12 views
-1

我有一个在网页上动态创建的表。该表有3列。'文件名','文件管理'和一个文本节点说'下载'的列。 这样javascript - 如何从动态表中获取值

filename  fileurl  download 
file1  www.pathtofile1 download 
file2  www.pathtofile2 download 
file3  www.pathtofile3 download 

要求这里有些事情是,如果用户点击一个特定的行中的“下载”,我应该得到该行的“文件名”和“fileurl”的相应值。 我该怎么做?

+0

你需要编写与您的要求满足码。咄 –

回答

0

这应该足以

$("table tr .download").click(function(){ 
 
    var row = $(this).closest("tr"); 
 
    var name = row.find(".name").html(), 
 
     url = row.find(".url").html(); 
 
    console.log(name,url) 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
<tbody> 
 
<tr> 
 
    <td class="name">file1</td> 
 
    <td class="url">www.pathtofile1</td> 
 
    <td class="download">Download</td> 
 
</tr> 
 
<tr> 
 
    <td class="name">file2</td> 
 
    <td class="url">www.pathtofile2</td> 
 
    <td class="download">Download</td> 
 
</tr> 
 
<tr> 
 
    <td class="name">file3</td> 
 
    <td class="url">www.pathtofile3</td> 
 
    <td class="download">Download</td> 
 
</tr>