可以获取我的表值(getElementById不起作用)

发布于 2025-01-26 11:36:18 字数 2619 浏览 3 评论 0原文

一旦单击“ Resererver”按钮(或单击'如果很重要'),我正在尝试获取动态表的行输入) 这是代码

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>

I'm trying to get a line of a dynamic table once the " reserver" button is clicked (or the line itself is clicked 'if that matters')but the 'getElementById' doesn't return anything ( i want to display them in an input)
here is the code

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>";
          }
        }; 
      }
         ?>

the 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

澉约 2025-02-02 11:36:18

您已经在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>";
          }
        }; 
      }
         ?>

You have been declared the JavaScript inside the JS file itself. in this case, The JS file Won't run for your HTML and won't affect anything written in your HTML. Also, you have declared your onClick function in the wrong way. To do this, you need to move the tag inside your HTML. For a better demonstration:

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>";
          }
        }; 
      }
         ?>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文