我正在编写一个运行for
循环的PHP脚本。我正在循环内创建多个表,我想根据循环动态命名表。
我已经尝试了以下代码,但是不起作用:
<table id="gameweek_history<? echo $i; ?>">
<script>function doCSV<? echo $i; ?>() {
var table1 = document.getElementById("gameweek_history<? echo $i; ?>").innerHTML;
<button onclick="doCSV(<? echo $i; ?>)">Export HTML Table To CSV File</button>
您能告诉我我需要更改什么吗?
答案 0 :(得分:0)
<?php
$count = 3;
for($i=0; $i<$count; $i++) {
?>
<table id ="gameweek_history<?php echo $i; ?>">
<script>function doCSV<?php echo $i; ?>() {
var table1 = document.getElementById("gameweek_history<?php echo $i; ?>").innerHTML;
<button onclick="doCSV(<?php echo $i; ?>)">Export HTML Table To CSV File</button>
</table>
<?php
}
?>
给出输出:
<table id ="gameweek_history0">
<script>function doCSV0() {
var table1 = document.getElementById("gameweek_history0").innerHTML;
<button onclick="doCSV(0)">Export HTML Table To CSV File</button>
</table>
<table id ="gameweek_history1">
<script>function doCSV1() {
var table1 = document.getElementById("gameweek_history1").innerHTML;
<button onclick="doCSV(1)">Export HTML Table To CSV File</button>
</table>
<table id ="gameweek_history2">
<script>function doCSV2() {
var table1 = document.getElementById("gameweek_history2").innerHTML;
<button onclick="doCSV(2)">Export HTML Table To CSV File</button>
</table>
答案 1 :(得分:0)
您应该从JavaScript中删除PHP代码。这样写:
<script>function doCSV(i) {
var table1 = document.getElementById("gameweek_history"+i).innerHTML;
// do something here with table1
</script>
<table id ="gameweek_history<? echo $i; ?>">
<button onclick="doCSV(<? echo $i; ?>)">Export HTML Table To CSV File</button>