Jquery 查找表的第一行

Jquery 查找表的第一行,jquery,Jquery,我试着只得到我桌子的第一个TR。如果我在每行中单击“删除”按钮,它仍然可以工作。我搞不懂!我想在提交后删除第一行。。。除了第一个,所有的作品!我在我的SQL for Ids标记中为myTable的行使用相同的Id来删除它们。。。不知道我是否清楚 <table id ="myTable" border="0" cellpadding="0" cellspacing="0" width="100%" class="scrollTable1"> <thead class="

我试着只得到我桌子的第一个TR。如果我在每行中单击“删除”按钮,它仍然可以工作。我搞不懂!我想在提交后删除第一行。。。除了第一个,所有的作品!我在我的SQL for Ids标记中为myTable的行使用相同的Id来删除它们。。。不知道我是否清楚

<table id ="myTable" border="0" cellpadding="0" cellspacing="0" width="100%" class="scrollTable1">

    <thead class="fixedHeader1">
    <tr>
        <th><center><a href="#">Id</a></center></th>
        <th><center><a href="#">User</a></center></th>
        <th><center><a href="#">Dest</a></center></th>
                    <th><center><a href="#">Msg</a></center></th>
                    <th><center><a href="#">Title</a></center></th>
                    <th><center><a href="#">Date</a></center></th>
                    <th><center><a href="#">Hour</a></center></th>
                    <th><center><a href="#">Langue</a></center></th>
                    <th><center><a href="#">Del</a></center></th>
    </tr>
</thead>

    <tbody class="scrollContent1">

 <?  

      $sql="SELECT * FROM messages ORDER BY username";
  $result = mysqli_query($con,$sql);          
  while($row = mysqli_fetch_array($result)) {

           echo'<tr id="'.$row[messageid].'">                           
           <td><center>'.$row[messageid].'</center></td>               
           <td><center>'.addslashes(utf8_decode($row[username])).'</center></td>
           <td><center>'.addslashes(utf8_decode($row[dest])).'</center></td>
                       <td><center>'.utf8_decode($row[message]).'</center></td>
                       <td><center>'.$row[titre].'</center></td>
                       <td><center>'.$row[date].'</center></td>
                       <td><center>'.$row[heure].'</center></td>
                       <td><center>'.$row[langue].'</center></td>
           <form name="ajaxform" id="ajaxform" action="delete_msg_SQL.php" method="POST">

                       <td><center><input type="button" class= "myButton" id="'.$row[messageid].'" value="X"/><input type="button" style="visibility:hidden" id="simple-post"/></center></td>
                       </form>
            </tr>';


  }



 ?>
    </tbody>
</table>

Jquery代码:

<script>

$(function(){
$('.myButton').click(function() {

    var myId = $(this).attr('id');
    $("#ajaxform").append('<input type="hidden" name="id" value="'+ myId + '"/>');             

      // HERE the PROBLEM If I click on the delete button on each rows it works anyway instead of the first one! 
      if ($(this).children('tr:first')) {
        alert('ok');            
      }
      $( '#simple-post' ).click(); 
      $('#'+ myId).remove();        
}); 
});
</script>

$(函数(){
$('.myButton')。单击(函数(){
var myId=$(this.attr('id');
$(“#ajaxform”)。追加(“”);
//这里的问题是,如果我在每一行上单击“删除”按钮,它无论如何都会工作,而不是第一行!
if($(this.children('tr:first')){
警报(“正常”);
}
$(“#简单帖子”)。单击();
$('#'+myId).remove();
}); 
});

编辑确定,然后执行以下操作:

NodeList.prototype.indexOf = function(x){
 for(var i=0;i<this.length;i++) if(this[i] == x) return i;
 return -1;
}
function del(o){ 
 var table = o.parentNode.parentNode.parentNode;
 var row = o.parentNode.parentNode;
 table.deleteRow(table.childNodes.indexOf(row));
}
...
...<td><input type="button" value="Delete row" onclick="del(this);" /></td>...
然后,带有“删除”按钮的表格单元格应如下所示:

<td><center><input type="button" class= "myButton" id="'.$row[messageid].'" value="X" onclick="del(this);"/>...
。。。
尝试而不是

$('#'+ myId).remove();
把这个密码

$(this).closest('tr').remove()
它将(从按钮元素)向上遍历树,并在途中删除第一个tr

要检查第一行,请尝试此选项

var tr = $(this).closest('tr')[0];

if (tr === $('#myTable tr:eq(1)')[0]) {
    alert('you clicked a button in the first row');
}
这里的工作示例

不太清楚您在这里想做什么。。。单击按钮,然后说
if($(this).children('tr:first'))
?这是行不通的,一个按钮不会有任何表行的子项。是的,你完全正确!!!大错特错!但是,如果在第一行单击此按钮,我可以从中了解到吗?您的问题非常模糊,而且您呈现的HTML无效,ID在文档上下文中必须是唯一的。为什么要在这里为每一行使用ID?
if($(this).children('tr:first')){
-
$(this)
表示按钮本身没有
子项。这将删除标题,应该是
.deleteRow(1)
嗯,是的,对不起,我可能在读取他的源代码时投入了不完全足够的时间。^这将删除表中的第一行,但包含按钮的行需要删除。对不起,我之前不太理解。它应该只是一个提示-现在这里有一个完整的函数,可以完成我所需要的我想你是在追求。我可以访问该函数并使用警报进行检查,但没有删除任何内容!我尝试过,但我遇到了完全相同的问题,我无法删除第一个,而且我无法提交!!!@user3281883你将在任何地方使用无效的HTML。你必须首先使用唯一的ID修复它(或者最好不要使用ID)顺便说一句,表单元素不能是TR的直接子元素。@user3281883,第一个
TR
包含
th
。您需要删除它吗?没有
myButton
是第一个
TR
。我以前做过,并使用了“$(this).closest('TR')。remove()'没有ID,但我仍然有同样的问题。关于窗体的子级,但关于表,您是对的?我尝试使用'$(“#myTable tr:nth child(1)”)。remove();'有效地删除了第一个tr(标题),然后我将其递增1以获得第二行,但它没有工作!
var tr = $(this).closest('tr')[0];

if (tr === $('#myTable tr:eq(1)')[0]) {
    alert('you clicked a button in the first row');
}