获取html表格指定行的元素

点击按钮(这里暂时用是编辑按钮)
关键代码在tr、button和script那里

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>数据表管理页面</title>
<style>
	.tftable {font-size:12px;color:#333333;width:100%;border-width: 1px;border-color: #ebab3a;border-collapse: collapse;table-layout: fixed;}
	.tftable th {font-size:12px;background-color:#e6983b;border-width: 1px;padding: 8px;border-style: solid;border-color: #ebab3a;text-align:left;}
	.tftable tr {background-color:#ffffff;}
	.tftable td {font-size:12px;border-width: 1px;padding: 8px;border-style: solid;border-color: #ebab3a;}
	.tftable tr:hover {background-color:#ffff99;}
</style>
</head>
 
<body>
<h1 style="text-align: center">学生信息表</h1>
<table class="tftable">
	<tr>
		<th>学号</th>
		<th>姓名</th>
		<th>性别</th>
		<th>专业</th>
		<th>班级</th>
		<th>操作</th>
	</tr>

<?php
$dbms='mysql';     //数据库类型
$host='localhost'; //数据库主机名
$dbName='classmanager';    //使用的数据库
$user='root';      //数据库连接用户名
$pass='';          //对应的密码
$dsn="$dbms:host=$host;dbname=$dbName";
$num=0;
$conn = new PDO($dsn, $user, $pass); //初始化一个PDO对象
//echo("连接成功\n");
$conn->query("set names utf8");
foreach ($conn->query('SELECT * from student') as $row) {
	$rowidd = 'stu'.$num;
	echo("<tr id=".$rowidd."><td>"
		 .$row[0]."</td><td>"
		 .$row[1]."</td><td>"
		 .$row[2]."</td><td>"
		 .$row[3]."</td><td >"
		 .$row[4]."</td><td >"
		 ."<button onClick='stuVisible();'>添加</button>"
		 ."<button onClick='edit($rowidd);'>编辑</button>"
		 ."<button>删除</button>"
		 ."</td></tr>");
	$num++;
}
?>
</table>
<div id="sf"; style="margin-left: 40%; display:none;">
	<form name="studentForm" action="s_insert.php" onsubmit="return check()" method="post">
		学号: <input type="text" name="sno" value="123456"><br />
		姓名: <input type="text" name="sname" value="123456"><br />
		性别: <input type="text" name="sgender" value="123456"><br />
		专业: <input type="text" name="major" value="123456"><br />
		学院: <input type="text" name="classno" value="123456"><br />
		<input type="submit" value="添加">
	</form>
</div>
<script>
	function stuVisible(){
		document.getElementById('sf').style.display='block';
	}
	function edit(rid){
		alert(rid.cells[0].innerHTML);
	}
</script>
</body>
</html>


发布了10 篇原创文章 · 获赞 2 · 访问量 5261

猜你喜欢

转载自blog.csdn.net/qq_36686441/article/details/94129140