2012-03-23 90 views
0

我有一个php表,我希望能够删除我选择的行,但我不知道如何去做这件事。这是我删除的javascript:php javascript删除单个行

// When a delete button is pressed in one of the rows 
function deleteCurrentRow(whatrow) 
{ 
    //deletes the approriate row according to what button was pressed 
    if (hasLoaded) { 
    var delRow = whatrow.parentNode.parentNode; 
    deleteRows(delRow);//sends the row for deletion to the function to be deleted 
    } 
} 



function deleteRows(rowforDeletion) 
{ 
    if (hasLoaded) { 

     var rowPos = rowforDeletion.sectionRowIndex;//postion of the row for deletion 
     rowforDeletion.parentNode.deleteRow(rowPos);//deletes row 
     increment = increment -1; 

    } 
} 

这里是我的PHP表:

<table border="1" cellspacing="5" id="tblmarkscheme" style="float: center;"> 
    <thead> 
    <tr> 
     <th colspan="5">Template</th> 
    </tr> 
    <tr> 
     <th>Mark</th><th>Criteria</th><th>Delete</th> 
    </tr> 
    </thead> 
    <tbody> 
    <?php 
     $query = "SELECT maxMark, criteria FROM mark "; 
     $result = mysql_query($query); 


     while ($row = mysql_fetch_array($result)) 
     { 
     echo "<tr>"; 
     echo "<td>" ."<input type = text name = text size = 1 id = mark value = ". $row['maxMark'].">" ."</td>"; 
     echo "<td>"."<textarea>".$row['criteria']. "</textarea>"."</td>"; 
     echo "<td>"."<input type = button value = Delete onclick = deleteCurrentRow(this);/>"."</td>"; 
     echo "</tr>"; 

     } 


    ?> 

    </tbody><!--elements added onload and when button pressed --> 
</table> 

我想删除表中的各行有一个按钮,按下

+0

我太傻了,所有我需要做的是把回报,并添加'它 – 2012-03-23 13:55:45

回答

0

这里是一个例子;如果你点击一个按钮,它的行被删除:

<!DOCTYPE HTML> 
<html> 
    <head> 
     <script type="text/javascript"> 
      var p = { 
       deleteRow: function(row) { 
       document.getElementById("mytable").deleteRow(row.rowIndex); 
       } 
      }; 
     </script> 
    </head> 
    <body> 
     <table id="mytable"> 
      <tr> 
       <td><input type="button" value="row1" onclick="p.deleteRow(this)"/></td> 
      </tr> 
      <tr> 
       <td><input type="button" value="row2" onclick="p.deleteRow(this)"/></td> 
      </tr> 
     </table> 
    </body> 
</html> 
0

其实这是一个HTML表。

您将需要使用javascript来删除行。

http://www.w3schools.com/jsref/met_table_deleterow.asp

+0

W3C嘘! HSSS!只是开玩笑你的回应可能会被预定,但在许多情况下,这是一个合适的参考,不幸的是,很酷的孩子认为这很糟糕,所以很多人说它是坏的,因为他们读了一段。 – CBusBus 2012-03-23 13:33:21

+0

,但theres php表行数据里面,我的javacode将工作表外行的PHP行,但不在 – 2012-03-23 13:34:42

+0

我只是把它,因为它是第一件事,当我GOOGLE“删除表中的行在JavaScript中......”而且我给OP带来了怀疑的好处,即如果想要更好/更多的信息,他就会知道这一点。 – 2012-03-23 13:34:51

0
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script> 
function del(id) 
{ 

var info = 'id=' + id; 
    if(confirm("Are you sure you want to delete this Record?")){ 
     var html = $.ajax({ 
     type: "GET", 
     url: "deletCourse.php", 
     data: info, 
     async: false , 
     success: function() { 
    window.location.reload(true);} 
     }).responseText; 


    } 
} 
</script> 

<?php 
$link=mysql_connect("localhost","root","") or die(mysql_error()); 
mysql_select_db("cart"); 
$sql=mysql_query("SELECT * FROM `details`"); 
echo "<table>"; 
echo "<tr><th>Name</th><th>NO of Items</th></tr>"; 
while($row = mysql_fetch_assoc($sql)){ 
    echo '<tr class="record">'; 
    echo "<td>" . $row['name'] . "</td>"; 
    echo "<td>" . $row['num'] . "</td>"; 

    echo '<td><div align="center"><a href="#" id="' . $row['id'] . '" onclick="del('.$row['id'].')">delete</a></div></td>'; 
    echo '</tr>'; 
} 
echo "</table>"; 
mysql_close($link); 
?>`` 
在onclick位
+0

请添加一些解释以改善答案。代码只是不太好。 – 2015-01-18 16:47:17