如何编写显示和隐藏密码输入框的代码

huangapple go评论88阅读模式
英文:

how to write a code of show and hide password input field

问题

以下是要翻译的代码部分:

function myFunction() {
    var data = document.getElementById("myInput");
    if (data.type === "password") {
        data.type = "text";
    } else {
        data.type = "password";
    }
}
<div class="form-group col-md-6">
    <label class="control-label">密码</label>
    <input type="password" name="password password1" id="myInput" class="form-control eye" value="{{old('password')}}">
    <i class="fa fa-eye" aria-hidden="true" onclick="myFunction()">显示/隐藏</i>
    {!! $errors->first('password', '<p class="text-warning errorBag">:message</p>') !!}
</div>
英文:

In this question there are the following answers of this question but i give the best answer of this question.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  function myFunction() {
      var data = document.getElementById(&quot;myInput&quot;);
      if (data.type === &quot;password&quot;) {
        data.type = &quot;text&quot;;
      } else {
        data.type = &quot;password&quot;;
      }
    }

<!-- language: lang-html -->

&lt;div class=&quot;form-group col-md-6&quot;&gt;
&lt;label class=&quot;control-label&quot;&gt;Password&lt;/label&gt;
&lt;input type=&quot;Password&quot; name=&quot;password password1&quot; id=&quot;myInput&quot; class=&quot;form-control eye&quot; value=&quot;{{old(&#39;password&#39;)}}&quot;&gt;

<i class="fa fa-eye" aria-hidden="true" onclick="myFunction()"> Show Hide</i> {!! $errors->first('password', '<p class="text-warning errorBag">:message</p>') !!}
</div>

<!-- end snippet -->

Share my answer please

答案1

得分: 1

这是一个更简洁的方法:

var input = document.getElementById("myInput");
function myFunction()
{ input.type = input.type === "password" ? "text" : "password" }
英文:

Here is a shorter approach:

    var input = document.getElementById(&quot;myInput&quot;);
    function myFunction()
    { input.type = input.type === &quot;password&quot; ? &quot;text&quot; : &quot;password&quot; }
   

答案2

得分: 0

function myFunction() {
  var data = document.getElementById("myInput");
  var curType = data[0].getAttribute("type");
  if (curType === "password") {
    data[0].setAttribute("type", "text");
  } else {
    data[0].setAttribute("type", "password");
  }
}
英文:
function myFunction() {
  var data = document.getElementById(&quot;myInput&quot;);
  var curType = data[0].getAttribute(&quot;type&quot;);
  if (curType === &quot;password&quot;) {
    data.[0].setAttribute(&quot;type&quot;,&quot;text&quot;);
    
  } else {
    data.[0].setAttribute(&quot;type&quot;,&quot;password&quot;);
    
  }
}

答案3

得分: 0

我在你的代码中没有发现任何错误或问题,你可以使用以下链接查看答案:https://www.w3schools.com/howto/howto_js_toggle_password.asp

英文:

I am not finding any mistake or wrong in your code, You can use following link for your answer: https://www.w3schools.com/howto/howto_js_toggle_password.asp

答案4

得分: 0

试一试:

<script type="text/javascript">
    $(document).ready(function () {
        $('#show_password').hover(function show() {
            // 将属性更改为文本
            $('#txtPassword').attr('type', 'text');
            $('.icon').removeClass('fa fa-eye-slash').addClass('fa fa-eye');
        },
        function () {
            // 将属性更改回密码
            $('#txtPassword').attr('type', 'password');
            $('.icon').removeClass('fa fa-eye').addClass('fa fa-eye-slash');
        });
        // 复选框显示密码
        $('#ShowPassword').click(function () {
            $('#Password').attr('type', $(this).is(':checked') ? 'text' : 'password');
        });
    });
</script>
英文:

Try This:

&lt;script type=&quot;text/javascript&quot;&gt;  
            $(document).ready(function () {  
                $(&#39;#show_password&#39;).hover(function show() {  
                    //Change the attribute to text  
                    $(&#39;#txtPassword&#39;).attr(&#39;type&#39;, &#39;text&#39;);  
                    $(&#39;.icon&#39;).removeClass(&#39;fa fa-eye-slash&#39;).addClass(&#39;fa fa-eye&#39;);  
                },  
                function () {  
                    //Change the attribute back to password  
                    $(&#39;#txtPassword&#39;).attr(&#39;type&#39;, &#39;password&#39;);  
                    $(&#39;.icon&#39;).removeClass(&#39;fa fa-eye&#39;).addClass(&#39;fa fa-eye-slash&#39;);  
                });  
                //CheckBox Show Password  
                $(&#39;#ShowPassword&#39;).click(function () {  
                    $(&#39;#Password&#39;).attr(&#39;type&#39;, $(this).is(&#39;:checked&#39;) ? &#39;text&#39; : &#39;password&#39;);  
                });  
            });  
        &lt;/script&gt;  

答案5

得分: 0

&lt;div class=&quot;form-group col-md-6&quot;&gt;
&lt;label class=&quot;control-label&quot;&gt;密码&lt;/label&gt;
&lt;input type=&quot;password&quot; name=&quot;password password1&quot; id=&quot;myInput&quot; class=&quot;form-control eye&quot; value=&quot;{{old(&#39;password&#39;)}}&quot;&gt;
&lt;button class=&quot;fa fa-eye&quot; aria-hidden=&quot;true&quot; onclick=&quot;myFunction()&quot;&gt; 显示/隐藏&lt;/button&gt;
&lt;/div&gt;
function myFunction() {
    var data = document.getElementById(&quot;myInput&quot;);
    if (data.type === &quot;password&quot;) {
        data.type = &quot;text&quot;;
    } else {
        data.type = &quot;password&quot;;
    }
}
英文:

you can try this

html code

&lt;div class=&quot;form-group col-md-6&quot;&gt;
&lt;label class=&quot;control-label&quot;&gt;Password&lt;/label&gt;
&lt;input type=&quot;Password&quot; name=&quot;password password1&quot; id=&quot;myInput&quot; class=&quot;form-control eye&quot; value=&quot;{{old(&#39;password&#39;)}}&quot;&gt;
&lt;button class=&quot;fa fa-eye&quot; aria-hidden=&quot;true&quot; onclick=&quot;myFunction()&quot;&gt; Show Hide&lt;/button&gt;                                                     &lt;/div&gt;

css code

 function myFunction() {
      var data = document.getElementById(&quot;myInput&quot;);
      if (data.type === &quot;password&quot;) {
        data.type = &quot;text&quot;;
      } else {
        data.type = &quot;password&quot;;
      }
    }

huangapple
  • 本文由 发表于 2020年1月6日 21:07:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定