为啥所有按钮只控制同一行数据?应该怎么解决啊。<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script language="javascript" type="text/javascript">
         function showtd(num) {            
             document.getElementById("bg"+num).style.display = "none";
             document.getElementById("show"+num).style.display = "block";
             document.getElementById("ba"+num);
             document.getElementById("gn"+num);
         }
         function hidetd(num) {
             document.getElementById("bg"+num).style.display = 'block';
             document.getElementById("show"+num).style.display = 'none';
         }
function showtext(){
document.getElementById("a").removeAttribute("readOnly");
document.getElementById("b").removeAttribute("readOnly");
document.getElementById("c").removeAttribute("readOnly");
document.getElementById("d").removeAttribute("readOnly");
document.getElementById("e").removeAttribute("readOnly");
document.getElementById("f").removeAttribute("readOnly");
}
function hidetext(){
document.getElementById("a").readOnly="true";
document.getElementById("b").readOnly="true";
document.getElementById("c").readOnly="true";
document.getElementById("d").readOnly="true";
document.getElementById("e").readOnly="true";
document.getElementById("f").readOnly="true";
}     </script>
</head>
<body>
<form>
<table border="1">
<?php
$db = mysql_connect('localhost','root','root');
mysql_select_db('ec',$db);
if (!$db)
  {
  die('Could not connect: ' . mysql_error());
  }
$result = mysql_query("select * from goods");
echo "<tr>
<th>GoodsID</th>
<th>BarCode</th>
<th>GoodsName</th>
<th>Category</th>
<th>Specifications</th>
<th>Manufacturers</th>
<th>Numbers</th>
<th>Instructions</th>
<th>Pictures</th>
<th>Update</th>
<th>Delete</th>
</tr>";
$n = 0;
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . "<input type='text' id='a' value='$row[barcode]' style='width:60px;border:0px' readOnly>" . "</td>";
  echo "<td>" . "<input type='text' id='b' value='$row[goods_name]' style='width:90px;border:0px' readOnly>" . "</td>";
  echo "<td>" . $row['category'] . "</td>";
  echo "<td>" . "<input type='text' id='c' value='$row[specifications]' style='width:90px;border:0px' readOnly>" . "</td>";
  echo "<td>" . "<input type='text' id='d' value='$row[manufacturers]' style='width:90px;border:0px' readOnly>" . "</td>";
  echo "<td>" . "<input type='text' id='e' value='$row[number]' style='width:60px;border:0px' readOnly>" . "</td>";
  echo "<td>" . "<input type='text' id='f' value='$row[instruction]' style='width:80px;border:0px' readOnly>" . "</td>";
  echo "<td>" . "<img src = '$row[picture_url]' style='width:80px;height=60px'/>" . "</td>";
  echo "<td id='bg$n'>".
      "<input id='btnshow$n' type='button' value='Update' onclick='showtd($n);showtext()' />".
      "</td>".
      "<td id='show$n' style='display:none'>".
          "<input id='btnclose$n' type='button' value='Save' onclick='hidetd($n);hidetext()'/>".
      "</td>";
  echo "<td>"."<a href='delete.php?id=$row[id]'>"."Delete"."</a>"."</td>";
    echo "</tr>";
   ++$n;
  }

mysql_close($db);
?>
</table>
</form>
</body>
</html>

解决方案 »

  1.   

    循环输出有多少个id='a'呢?然后 document.getElementById("a") 会得到什么……
      

  2.   

    什么情况?数据库读出来ID都对的吧?<a href='delete.php?id=$row[id]'>这个应该id有传过去吧,再试试吧~
      

  3.   

    a标签读出来的id是我在数据库内设置的主键,和<input >d的id是两码事。
      

  4.   

    你每行同一列的 id 都相同,怎么能用 getElementById 来定位呢?
      

  5.   

    65-71行 设置的id都是重复的 如果区分的话可以把设置的ID(a,b,c...)前拼接上$row['id']加以区分