简体   繁体   English

如何获得点击表行的值?

[英]how to get the value of the clicked table row?

My question is: how do I get the value of the clicked row and column? 我的问题是:如何获得点击的行和列的值?

The code I have is this: 我的代码是这样的:

JS: JS:

$.ajax({
        type: 'POST',
        url: url,
        data: json,
        success: function(data) {
            var response_array = JSON.parse(data);
            var columns = ['id', 'name', 'email', 'telephone', 'website', 'city'];
            var table_html = ' <tr>\n' +
                '<th id="id">Id</th>\n' +
                '<th id="name">Bedrijfnaam</th>\n' +
                '<th id="email">E-mail</th>\n' +
                '<th id="telephone">Telefoon</th>\n' +
                '<th id="website">Website</th>\n' +
                '<th id="city">Plaats</th>\n' +
                '</tr>';
            for (var i = 0; i < response_array.length; i++) {
                //create html table row
                table_html += '<tr>';
                for (var j = 0; j < columns.length; j++) {
                    //create html table cell, add class to cells to identify columns
                    table_html += '<td class="' + columns[j] + '" >' + response_array[i][columns[j]] + '</td>'
                }
                table_html += '</tr>'
            };
            $("#posts").append(table_html);
        },
        error: function (jqXHR, textStatus, errorThrown) { alert('ERROR: ' + errorThrown); }
    });

Here is the HTML: 这是HTML:

<div class="tabel">
     <table id="posts">
     </table>
</div>

I have tried the following: 我尝试了以下方法:

 $('#posts').click(function(){
        console.log("clicked");
        var id = $("tr").find(".id").html();
        console.log(id);
});

Sadly this will only give me the id of the first row, no matter where I click. 不幸的是,无论我在哪里单击,这只会给我第一行的ID。

Any help is appreciated! 任何帮助表示赞赏!

Ramon 拉蒙

The below approach should be able to find the ID 下面的方法应该能够找到ID

$('#post').on('click', function(e){
var id = $(e.target).closest('tr').find(".id").html();
console.log(id)
})

HTML content of clicked row 点击行的HTML内容

 $('#posts tr').click(function(){
       $(this).html();
 });

text from clicked td 点击的td中的文字

 $('#posts tr td').click(function(){
        $(this).text();
 });

If you are using ajax and you are redrawing elements , you will not catch em via click function. 如果您正在使用ajax并且正在重绘元素 ,则不会通过click函数捕获em。 You will need to add on function: 您将需要添加功能:

$(document).on('click','#posts tr td','function(){
      $(this).text();
});

You may try to use AddEventListener for your table, it will work for sure. 您可以尝试为表使用AddEventListener,它肯定可以工作。 Like this: 像这样:

let posts = document.getlementById('posts');
posts.addEventListener('click',(e) => {
// anything you need here, for example:
console.log(e.target);
e.preventDefault();
});

As well - it will be fine not to use same IDs for elements in a grid (like id="id" which you have), it should be different. 同样,最好不要对网格中的元素使用相同的ID(例如您拥有的id =“ id”),这应该有所不同。

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

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