如何使用Html帮助器在mvc中显示密码隐藏字段

时间:2017-12-28 12:06:15

标签: javascript c# jquery asp.net-mvc asp.net-mvc-4

@Html.Password("pasword", null, new { @class = "form-control frmField", placeholder = "Password" })

我正在使用这个,我希望这里应该是我点击的一个按钮,密码变得可见。我知道这将通过jquery或Javascript,但我无法理解在mvc中。如何在mvc中应用该技术?

<input data-toggle="password" type="password"  data-placement="after" data-eye-class="glyphicon" data-eye-open-class="glyphicon glyphicon-eye-open" data-eye-close-class="glyphicon glyphicon-eye-close" data-eye-class-position="true" class="form-control pwd">
<input type="text" class="form-control" placeholder="password" style="display:none;" /> 

我用过这个并且效果很好。如何在mvc中实现它?

4 个答案:

答案 0 :(得分:3)

密码类型更改为文字类型

$( ".btnShow" ).mousedown(function() {
  $(".pwd").attr("type","text");
});
$( ".btnShow" ).on("mouseleave",function() {
  $(".pwd").attr("type","password");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input  type="password"  class="form-control pwd">
<input type="button" class="btnShow" value="show"/>

答案 1 :(得分:0)

$("#Password").attr("type","text")

只需将#Password替换为您的选择器,如果您想要将其更改回来,请执行以下操作:

$("#Password").attr("type","password")

答案 2 :(得分:0)

在您的网页中添加javascript以实现它。这是一个供您参考的样本。

@Html.Password("pasword", null, new { @class = "form-control frmField", placeholder = "Password" })
<input type="button" id="showHidePassword" value="Show" />

<script>
    $("#showHidePassword").click(function(){
         if($(this).val() == "Show"){
             $(this).val("Hide");
             $("#password").attr("type", "text");
         } else {
             $(this).val("Show");
             $("#password").attr("type", "password");
         }
    });
</script>

答案 3 :(得分:0)

使用以下行

将ID分配给密码字段
<input id="passwordField" data-toggle="password" type="password"  data-placement="after" data-eye-class="glyphicon" data-eye-open-class="glyphicon glyphicon-eye-open" data-eye-close-class="glyphicon glyphicon-eye-close" data-eye-class-position="true" class="form-control pwd">
<input type="checkbox" onclick="ShowHidePass(this)" />

将以下JS函数添加到脚本块

function ShowHidePass(objChk)
{
if(objChk.checked)
    passwordField.type="text"
  else
  passwordField.type="password"
}