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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程