duanhui4160 2017-05-07 10:19
浏览 100
已采纳

如何查看和隐藏表的所有行的密码

Table from the databaseThe show password function only works on the first row of the entry and is not working on the next entries/rows. The items are displayed by php through the database using foreach. how will the show/hide password work for all entries? any ideas?? I have the code as follows:

Html:

<td data-title="Password"><input id="viewPass" type="password" value="<?php echo $item["password"]; ?>" readonly/></td>
<button type="button" id="viewPswd" class="btn btn-default"><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span></button>
<script src="js/showPass.js"></script>

Javascript:

 var myButton = document.getElementById('viewPswd'),
  myInput = document.getElementById('viewPass');
  myButton.onclick = function () {

      'use strict';

      if (this.id === 'viewPswd') {
          myInput.setAttribute('type', 'text');
          this.textContent = 'Hide';

      } else {

           myInput.setAttribute('type', 'password');

           this.id = 'viewPswd';
      }
  };
  • 写回答

2条回答 默认 最新

  • douhu1990 2017-05-07 10:35
    关注

    If you want to work with multiple buttons and textboxes you have to assign a common name or class or indivdual id.

    You can't assign same id to all elements.

    Here i used name attribute

    var myButton = document.getElementsByName('dynamic');
    var myInput = document.getElementsByName('viewPass');
    myButton.forEach(function(element, index){
      element.onclick = function(){
         'use strict';
    
          if (myInput[index].type == 'password') {
              myInput[index].setAttribute('type', 'text');
              element.firstChild.textContent = 'Hide';
              element.firstChild.className = "";
    
          } else {
               myInput[index].setAttribute('type', 'password');
               element.firstChild.textContent = '';
                element.firstChild.className = "glyphicon glyphicon-eye-open";
          }
      }
    })
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <td data-title="Password">
    <input name="viewPass" type="password" value="abcd" readonly/></td>
    
    
    <button type="button" id="" class="btn btn-default" name="dynamic"><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span></button>
    <br>
    <td data-title="Password">
    <input name="viewPass" type="password" value="watha" readonly/></td>
    
    <button type="button" id="" class="btn btn-default" name="dynamic"><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span></button>
    <br>
    
    <td data-title="Password">
    <input name="viewPass" type="password" value="xyz" readonly/></td>
    
    <button type="button" id="" class="btn btn-default" name="dynamic"><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span></button>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大