dtrphb5597 2015-03-20 11:24
浏览 39
已采纳

使用javascript在没有Ajax / Jquery的情况下启用特定行中的文本框

So I have this table getting filled with information from database, The insert and delete functions are working but I need to be able to enable the text boxes in the specific row that the edit button is pressed. Unfortunatly I can not use Jquery or Ajax as this is a school project and we're not allowed to use it.

At the moment it gets the unique row ID from the database.

    <?php
    include("db.php");
    ?>

   <div class="ukeContainer"> <!-- Legger inn i ukecontainer -->
   <input type="button" value="Legg til ny bruker" onclick="nytt();">
   <table class="sortable">  
   <!-- gjør tabellen mulig å sortere -->
  <tr>
  <th>Epost</th>
  <th>Brukernavn</th>
  <th>Etternavn</th>
  <th>Fornavn</th>
  <th>Brukertype</th>
  <th></th></tr>


  <?php
  $sql = "select A.*, Br.Type from  Brukere A INNER JOIN Brukertyper Br    ON A.Brukertype=Br.Brukertype"; 
  if ($db) {
    $res = $db->query($sql);
     while ($rad = $res->fetch_assoc()) {
      echo("<tr><td><input type='text' name='epost'      value='".$rad['Epost']."' disabled></td>");
  echo("<td><input type='text' name='bnavn' value='".$rad['Brukernavn']."' disabled></td>");
  echo("<td><input type='text' name='enavn' value='".$rad['Etternavn']."' disabled></td>");
  echo("<td><input type='text' name='fnavn' value='".$rad['Fornavn']."' disabled></td>");
  echo("<td><input type='text' name='type' value='".$rad['Type']."' disabled></td>");
  echo("<td>");
  echo("<input id='Slett".$rad['idBrukere']."' ");
  echo("type='submit' value='Slett' onclick='slett(".$rad['idBrukere'].")';>");
  echo("<input id='Rediger".$rad['idBrukere']."' ");
  echo("type='submit' value='Rediger' onclick='endre(".$rad['idBrukere'].")';></td></tr>");
  echo("</td></tr>");
}
   } else {die("Får ikke forbindelse med database.");}
 ?>
    </table>
 </div>

 </body>
  • 写回答

1条回答 默认 最新

  • doukuai3822 2015-03-20 11:39
    关注

    Just out of the head, I'd do it like this:

    Give every an unique id. Then, at your edit-function, pass the ID of the row.

    Ok, so now we are in the editfunction and we know which row we want to enable.. We can now use the following JS code to achieve what you want:

    function enableRow(rowId)
    {
        // Get the container element
        container = document.getElementById(rowId);
    
        // Find its child `input` elements
        var inputs = container.getElementsByTagName('input');
        for (var index = 0; index < inputs.length; ++index) {
            inputs[index].disabled = false;
        }
    }
    

    I recovered part of the code from here: Get list of all `input` objects using JavaScript, without accessing a `form` object

    Hope that helps. :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法