为啥所有按钮只控制同一行数据?

WBOY
Release: 2016-06-23 14:03:48
Original
1011 people have browsed it

为啥所有按钮只控制同一行数据?应该怎么解决啊。

<!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="max-width:90%"/ alt="为啥所有按钮只控制同一行数据?" >" . "</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>
Copy after login


回复讨论(解决方案)

总觉得你这代码有一堆语法问题啊

总觉得你这代码有一堆语法问题啊
可以正常运行。

循环输出有多少个id='a'呢?

然后 document.getElementById("a") 会得到什么……

什么情况?数据库读出来ID都对的吧?这个应该id有传过去吧,再试试吧~

循环输出有多少个id='a'呢?

然后 document.getElementById("a") 会得到什么……
我也觉得是这个id的问题,就是不知道怎么改。

什么情况?数据库读出来ID都对的吧?这个应该id有传过去吧,再试试吧~
a标签读出来的id是我在数据库内设置的主键,和d的id是两码事。

引用 1 楼 chendong_j 的回复:
总觉得你这代码有一堆语法问题啊

 可以正常运行。 

顿时感觉lz好强大

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

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

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template