简体   繁体   English

jQuery使用PHP循环填充表的行

[英]Jquery Use Row of Table Populated By PHP in Loop

was hoping to have someone with jQuery expertise help me out here... 希望有jQuery专业知识的人在这里帮助我...

I have a table that is created dynamically through php using such code: 我有一个使用以下代码通过php动态创建的表:

...
  <tbody>
<?php
foreach ($members as $member){
  echo '<tr>';
    echo "<td><a href='#' class='edituser-information' title='edituser_information' data-id=" .$member['id']. "><img src='/images/edit-button.png' width='59' height='28' alt='EDIT'></a>";
  echo '</tr>';
}?>
  </tbody>
...

I am passing information into a jQuery function and then ajax using this: 我将信息传递到jQuery函数,然后使用此Ajax:

$('#editUsersTable tbody').on('click', 'tr', function (){           
  data_id = $('td a.edituser-information', this).eq(0).attr('data-id');
  $.ajax({
   url: 'edit_specific_user.php',
   type: 'POST',
   data: {id : data_id},
   ...

THE PROBLEM: 问题:

The ajax script picks up data-id correctly because it loads the information right through the row that was clicked on... Unfortunately, what I want is the user is FORCED to click the edit button, and not just the row in the table... Ajax脚本正确地获取了数据ID,因为它通过单击的行直接加载了信息。不幸的是,我想要的是用户被迫单击编辑按钮,而不仅仅是单击表中的行。 ..

As of now, the user can just click the row, and then it proceeds right into the jQuery because the entire row is the trigger. 到目前为止,用户只需单击该行,然后它就直接进入jQuery,因为entire row都是触发器。

How can I change it that the user must click the specific row's edit button, and not the row itself? 如何更改用户必须单击特定行的编辑按钮而不是行本身的按钮?

Thank you in advance!! 先感谢您!!

try 尝试

$('#editUsersTable tbody tr td').on('click', 'a.edituser-information', function (){           
    data_id = $(this).eq(0).attr('data-id');
    $.ajax({
    url: 'edit_specific_user.php',
    type: 'POST',
    data: {id : data_id},
    ...

You need to change the propogation selector in the very first line: 您需要在第一行更改传播选择器:

$('#editUsersTable tbody').on('click', 'tr', function (){  

changes to 更改为

$('#editUsersTable tbody').on('click', 'tr a', function (){  

Refer to the "Direct and Delegated events" section in the jQuery documentation for "on" 请参阅jQuery文档中的“直接和委派事件”部分,以了解“开启”

http://api.jquery.com/on/ http://api.jquery.com/on/

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

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