dqan70724 2016-10-14 14:19
浏览 21

使用onClick事件处理程序使用jQuery访问html表中的数据

I have a table that is being populated by a foreach loop to get data from a database:

foreach ($retrieve_data as $retrieved_data) {
    $content .= "
    <tr id=\"id_$retrieved_data->id\">
        <td> $retrieved_data->id </td>
        <td> $retrieved_data->posname </td>
        <td> $retrieved_data->month </td>
        <td> $retrieved_data->year </td>
        <td> $retrieved_data->availableinv </td>
        <td> $retrieved_data->maxinv </td>
        <td> <button type=\"button\" id=\"editImpressionsBtn\" value=\"Edit\">Edit </button>
    </tr>";
}

I want to write a jQuery function that gets data from the table like the id, availableinv, and maxinv when the edit button is clicked.

jQuery(document).on('click', 'editImpressionsBtn', function() {}

How can I assign a variable in this function to the data in each of these columns for only the row that the edit button was clicked?

  • 写回答

3条回答 默认 最新

  • doujiu8826 2016-10-14 14:27
    关注

    Something like this? One thing I noticed, you need to make sure to add the selector (. or # for class or ID) to the handler.

    $('.table_selector tbody').on('click', '.editImpressionsBtn', function(){ 
        row = $(this).closest('tr');
    
    })
    

    Then you can add values or ids and get them like this

    var my_id_value = row.attr('id')
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等