2014-02-08 6 views
-1

나는 내 테이블의 첫 번째 TR 만 가져 오려고합니다. 각 행의 삭제 버튼을 클릭하면 어쨌든 작동합니다. 나는 그것을 얻을 수 없다! 제출 후 맨 처음 행을 삭제하고 싶습니다 ... 첫 번째 행을 제외하고 모두 작동합니다! 난 ... 분명 해요 경우 몰라 ... 그들을 제거하기 위해 myTable에의 행에 대한 ID를 태그에 대한 내 SQL에서 동일한 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> 
+0

하지 정말 선택을 시도하십시오 수행하는 ... 버튼을 클릭하고'if ($ (this) .children ('tr : first'))'? 그게 작동하지 않을 것입니다, 버튼은 테이블 행인 자식을 가지지 않을 것입니다. – Joeytje50

+0

네가 완벽하게 맞았다! 큰 실수 ! 하지만이 버튼이 첫 번째 행에서 클릭되면 거기에서 갈 수 있습니까? – user3281883

+0

질문이 매우 모호하며 어쨌든 렌더링 된 HTML이 유효하지 않으며 ID가 문서 컨텍스트에서 고유해야합니다. 왜 각 행에 ID를 사용합니까? –

답변

0

을 그것은 나무를 가로지를 것이다. (버튼 요소에서) 위로 이동 한 후 첫 번째 tr을 제거합니다.

jQuery closest reference

는 첫 번째 행에 대한 검사가 여기에 뭘 하려는지 여기에이

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

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

작업 예를 JSBin

+0

나는 똑같은 문제를 일으켰다. 첫 번째 것을 제거 할 수없고, 제출할 수 없다. – user3281883

+0

@ user3281883 잘못된 HTML을 사용하면 어디에서나 사용할 수 있습니다. 고유 ID를 사용하여 문제를 해결하거나 ID를 전혀 사용하지 않는 것이 좋습니다. FORM 요소는 TR의 하위 항목이 될 수 없습니다. –

+0

@ user3281883 첫 번째 'tr'에는 'th'가 포함됩니다. 삭제해야합니까? 첫 번째'tr'에는'myButton'이 없습니다. –

0

편집 좋아,이 :

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>... 

편집 MK II : 좋아, 그 프로토 타입 확장을두고 있지만,에 델 기능을 변경해보십시오 이 :

function del(o){ 
var table = o.parentNode.parentNode.parentNode.parentNode.parentNode; 
var row = o.parentNode.parentNode.parentNode; 
table.deleteRow(table.childNodes.indexOf(row)); 
} 

그리고 삭제 버튼을 사용하여 테이블 셀은 다음과 같이한다 : 대신에이 코드

$(this).closest('tr').remove() 

을 넣어

$('#'+ myId).remove(); 

의 시도

<td><center><input type="button" class= "myButton" id="'.$row[messageid].'" value="X" onclick="del(this);"/>... 
+0

이것은 헤더를 제거 할 것이고, .deleteRow (1);이어야한다. –

+0

음, 그래, 미안하지만, 나는 그의 소스를 읽기에 충분하지 않은 시간을 투자했을지도 모른다. ^^ – ElDoRado1239

+0

예. 첫 번째 행의 경우 앞에 조건이 있어야합니다 ... – user3281883