无法获取我的 table 的值(getElementById 不工作)
Can't get the values of my table (getElementById not working)
我试图在单击“保留”按钮(或单击该行本身 'if that matters')后获取动态 table 行,但 'getElementById' 没有't return 任何东西(我想在输入中显示它们)
这是代码
HTML / PHP
<div class="form1">
<div>
<label>Code creneau1</label>
<input type="text" id="nop" name='nop'>
<label>Code creneau2</label>
<input type="text" id="seh" name='seh'>
<label>Code creneau3</label>
<input type="text" id="row" name='row'>
<label>Code creneau4</label>
<input type="text" id="name" name='name'>
</div>
<table class="table table-striped" id="res">
<thead>
<tr>
<th>code salle</th>
<th> type </th>
<th>etage</th>
<th>capacité</th>
<th> action</th>
</tr>
</thead>
<tbody><?php
while ($donnees = $requete->fetch() and $donnees2 = $requete2->fetch())
{
$donnees_ar=$_POST['date'];
$code_cr=$_POST['heure'];
// echo $donnees2['id_res'].'<br>';
if($donnees['code_salle']!=$donnees2['code_salle'] and $donnees2['date_res']=$_POST['date']){
echo "<tr id='try'>
<td >".$donnees['code_salle']."</td>
<td >".$donnees['type_salle']."</td>
<td >".$donnees['capacity']."</td>
<td >".$donnees['etage']."</td>
<td>
<a href='#0' id='he' class='cd-popup-trigger'>reserver</a>
</td>
</tr>";
}
};
}
?>
JS
<script src="js/main.js"></script>
<script>
var tbl= document.getElementById("res");
for(var x=1;x<tbl.rows.length;x++ ){
tbl.rows[x].onclick =function {
document.getElementById('name').value=this.cells[0].innerHTML;
document.getElementById('row').value=this.cells[1].innerHTML;
document.getElementById('seh').value=this.cells[2].innerHTML;
document.getElementById("nop").value=this.cells[3].innerHTML;
}
}
</script>
您已在 JS 文件本身中被声明为 JavaScript。在这种情况下,JS 文件不会 运行 用于您的 HTML,也不会影响您的 HTML 中写入的任何内容。此外,您以错误的方式声明了您的 onClick 函数。为此,您需要将标签移动到 HTML 中。为了更好的演示:
var table= document.getElementById("res");
if (table != null) {
for (var i = 0; i < table.rows.length; i++) {
for (var j = 0; j < table.rows[i].cells.length; j++)
table.rows[i].cells[j].onclick = function () {
tableText(this);
};
}
}
function tableText(tableCell) {
alert(tableCell.innerHTML);
}
<div class="form1">
<div>
<label>Code creneau1</label>
<input type="text" id="nop" name='nop'>
<label>Code creneau2</label>
<input type="text" id="seh" name='seh'>
<label>Code creneau3</label>
<input type="text" id="row" name='row'>
<label>Code creneau4</label>
<input type="text" id="name" name='name'>
</div>
<table class="table table-striped" id="res">
<thead>
<tr>
<th>code salle</th>
<th> type </th>
<th>etage</th>
<th>capacité</th>
<th> action</th>
</tr>
</thead>
<tbody>
<?php
while ($donnees = $requete->fetch() and $donnees2 = $requete2->fetch())
{
$donnees_ar=$_POST['date'];
$code_cr=$_POST['heure'];
// echo $donnees2['id_res'].'<br>';
if($donnees['code_salle']!=$donnees2['code_salle'] and $donnees2['date_res']=$_POST['date']){
echo "<tr id='try'>
<td >".$donnees['code_salle']."</td>
<td >".$donnees['type_salle']."</td>
<td >".$donnees['capacity']."</td>
<td >".$donnees['etage']."</td>
<td>
<a href='#0' id='he' class='cd-popup-trigger'>reserver</a>
</td>
</tr>";
}
};
}
?>
我试图在单击“保留”按钮(或单击该行本身 'if that matters')后获取动态 table 行,但 'getElementById' 没有't return 任何东西(我想在输入中显示它们) 这是代码
HTML / PHP
<div class="form1">
<div>
<label>Code creneau1</label>
<input type="text" id="nop" name='nop'>
<label>Code creneau2</label>
<input type="text" id="seh" name='seh'>
<label>Code creneau3</label>
<input type="text" id="row" name='row'>
<label>Code creneau4</label>
<input type="text" id="name" name='name'>
</div>
<table class="table table-striped" id="res">
<thead>
<tr>
<th>code salle</th>
<th> type </th>
<th>etage</th>
<th>capacité</th>
<th> action</th>
</tr>
</thead>
<tbody><?php
while ($donnees = $requete->fetch() and $donnees2 = $requete2->fetch())
{
$donnees_ar=$_POST['date'];
$code_cr=$_POST['heure'];
// echo $donnees2['id_res'].'<br>';
if($donnees['code_salle']!=$donnees2['code_salle'] and $donnees2['date_res']=$_POST['date']){
echo "<tr id='try'>
<td >".$donnees['code_salle']."</td>
<td >".$donnees['type_salle']."</td>
<td >".$donnees['capacity']."</td>
<td >".$donnees['etage']."</td>
<td>
<a href='#0' id='he' class='cd-popup-trigger'>reserver</a>
</td>
</tr>";
}
};
}
?>
JS
<script src="js/main.js"></script>
<script>
var tbl= document.getElementById("res");
for(var x=1;x<tbl.rows.length;x++ ){
tbl.rows[x].onclick =function {
document.getElementById('name').value=this.cells[0].innerHTML;
document.getElementById('row').value=this.cells[1].innerHTML;
document.getElementById('seh').value=this.cells[2].innerHTML;
document.getElementById("nop").value=this.cells[3].innerHTML;
}
}
</script>
您已在 JS 文件本身中被声明为 JavaScript。在这种情况下,JS 文件不会 运行 用于您的 HTML,也不会影响您的 HTML 中写入的任何内容。此外,您以错误的方式声明了您的 onClick 函数。为此,您需要将标签移动到 HTML 中。为了更好的演示:
var table= document.getElementById("res");
if (table != null) {
for (var i = 0; i < table.rows.length; i++) {
for (var j = 0; j < table.rows[i].cells.length; j++)
table.rows[i].cells[j].onclick = function () {
tableText(this);
};
}
}
function tableText(tableCell) {
alert(tableCell.innerHTML);
}
<div class="form1">
<div>
<label>Code creneau1</label>
<input type="text" id="nop" name='nop'>
<label>Code creneau2</label>
<input type="text" id="seh" name='seh'>
<label>Code creneau3</label>
<input type="text" id="row" name='row'>
<label>Code creneau4</label>
<input type="text" id="name" name='name'>
</div>
<table class="table table-striped" id="res">
<thead>
<tr>
<th>code salle</th>
<th> type </th>
<th>etage</th>
<th>capacité</th>
<th> action</th>
</tr>
</thead>
<tbody>
<?php
while ($donnees = $requete->fetch() and $donnees2 = $requete2->fetch())
{
$donnees_ar=$_POST['date'];
$code_cr=$_POST['heure'];
// echo $donnees2['id_res'].'<br>';
if($donnees['code_salle']!=$donnees2['code_salle'] and $donnees2['date_res']=$_POST['date']){
echo "<tr id='try'>
<td >".$donnees['code_salle']."</td>
<td >".$donnees['type_salle']."</td>
<td >".$donnees['capacity']."</td>
<td >".$donnees['etage']."</td>
<td>
<a href='#0' id='he' class='cd-popup-trigger'>reserver</a>
</td>
</tr>";
}
};
}
?>