简体   繁体   English

从特定列获取值

[英]Get the value from a particular column

I'm trying to alert message on click each column. 我正在尝试在单击每个列时提醒消息。 I have a table in which three row and four column, now I want alert message with next column value on third column click of each table. 我有一个包含三行四列的表,现在我想在每个表的第三列单击时显示警报消息,其中包含下一列的值。 I have tried this but all column get value not a particular column. 我已经尝试过了,但是所有列都获得值,而不是特定的列。 my problem is this is invoke in every column click but I want alert message only when third column click of each row. 我的问题是,这是在每列单击中调用,但是我仅在每行第三列单击时才需要警报消息。

HTML 的HTML

<table id='myTable'>
<tr><td>R1C1</td><td>R1C2</td><td>R1C3</td><td>R1C4</td></tr>
<tr><td>R2C1</td><td>R2C2</td><td>R2C3</td><td>R2C4</td></tr>
<tr><td>R3C1</td><td>R3C2</td><td>R3C3</td><td>R3C4</td></tr>
</table>

JS JS

$("#myTable tr").bind("click", function () {
     alert($(this).children("td").eq(3).html());
});

Demo Here 在这里演示

please try this code 请尝试此代码

 $(document).ready(function () {

            $('#myTable tr').each(function (Mindex, Mval) {
                $(Mval).find('td:eq(2)').click(function () {
                    alert($(Mval).find('td:eq(3)').html());
                });
            });
$("#myTable tr td:nth-child(3)").click(function () {
     alert($(this).next().html());
});

Try this 尝试这个

 $('table#myTable tr td:nth-child(3)').on('click', function() {
            alert($(this).next().html());
        });

Check Here 在这里检查

Try this: 尝试这个:

 $('#myTable').children('tr').children('td').eq(3).on('click', function() {
     alert($(this).next().html());
 });

The above function makes sure that you are calling the direct children elements but not any nested other same elements. 上面的函数确保您正在调用直接子元素,但不调用任何嵌套的其他相同元素。 Solves future regressions. 解决将来的回归问题。

If your table is going to stay the same size there are answers that already work. 如果您的桌子要保持原样,那么已经有答案了。 If your table is going to grow I would recommend doing some event delegation. 如果您的桌子越来越多,我建议您进行一些事件委托。 It will dramatically speed up the page if there are going to be a large number of event listeners. 如果将有大量的事件侦听器,它将大大加快页面的速度。

$('#myTable').on('click', 'td:nth-child(3)', function(){
    alert($(this).text());
});

See this jsfiddle , and this jquery documentation . 请参阅此jsfiddle和此jquery文档

below code will find and set the desired column value 下面的代码将找到并设置所需的列值

$('#myTable tr:gt(0)').each(function(){
       // console.log($('td:eq(3)', $(this)).html());
        alert($('td:eq(3)', $(this)).html());
        $('#myTable').find('tr:gt(0)').find('td:eq(3)').html("hello");
    });

Try this: 尝试这个:

var thirdEle = $("#myTable tr td:nth-child(3)");
thirdEle.bind("click", function () {
    alert($(this).html());
});

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

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