gpt4 book ai didi

Javascript HTML Table 根据另一个单元格的 onclick 获取第一列的单元格值

转载 作者:行者123 更新时间:2023-12-03 02:05:29 24 4
gpt4 key购买 nike

我正在制作一个 PHP Web 应用程序,并将数据从 MySql 提取到下面的 HTML 表中。

<table id = "table" width="500px" cellpadding=2 celspacing=5 border=2 >
<tr>
<th>Subject Code</th>
<th>Subject Name</th>
<th>Question Paper</th>
</tr>
<?php while($row=mysqli_fetch_array($result)):?>
<tr>
<td align="center"><?php echo $row['Subject_Code'];?></td>
<td align="center"><?php echo $row['Subject_Name'];?></td>
<td align="center"><a href="javascript:showPaper()"><img border="0" alt="image" src="ProjectPictures/questionPaper.png" width="30" height="30"></a></td>
</tr>
<?php endwhile;?>
</table>

这是表格的输出

enter image description here

第三列由可点击的图像图标组成,这些图标将试卷加载到旁边的 iFrame 中。

我希望当单击此图像时,获得该特定行的第一列的单元格值 -->“主题代码”。

这是代码:

function showPaper(){
var v;
//method1 not working
for(var i=1;i<table.rows.length; i++){
table.rows[i].onclick = function(){
rIndex = this.rowsIndex;
v=this.cells[0].innerHTML;
alert(v);
};
}
//method 2 not working
v = $("#table tr.selected td:first").html();
alert(v);
/*loc1 path rest of code
document.getElementById('myiFrame').src = loc1*/;
}

我需要此主题代码值作为 iFrame 的 pdf src 路径中的变量之一。

但似乎没有任何效果,“v”变量中的值显示未定义。

我怎样才能让这段代码工作?

最佳答案

您需要使用onclick()事件来获取Subject_Code的值。

改变

<td align="center"><a href="javascript:showPaper()"><img border="0" alt="image" src="ProjectPictures/questionPaper.png" width="30" height="30"></a></td>

<td align="center" id="<?php echo $row['Subject_Code'];?>" onclick="showPaper(this.id);"><img border="0" alt="image" src="ProjectPictures/questionPaper.png" width="30" height="30"></td>

你的showPaper()应该是

<script>
function showPaper(id)
{
alert(id);
//Do something
}
</script>

关于Javascript HTML Table 根据另一个单元格的 onclick 获取第一列的单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49828290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com