在MVC Razor View中实现Jquery.ShowPassword

所有发生的事情都是密码盒刚刚消失,并在选中复选框时重新出现。 剃刀视图中的代码:
<div id="passtext" class="editor-field">
    @Html.PasswordFor(m => m.Password)
    @Html.ValidationMessageFor(m => m.Password)
</div>

            <div class="editor-label">
                <input id="passcheckbox" type="checkbox" /><label>Show?</label>
                @Html.CheckBoxFor(m => m.RememberMe)
                @Html.LabelFor(m => m.RememberMe)
            </div>
我在底部的@renderpartail中有Javascript函数:
@section Scripts {
<script type="text/javascript">
    $('#text').showPassword('#checkbox');
</script>
}
并且jquery.showpassword-1.0.js包含在视图顶部和其他脚本:
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.showpassword-1.0.js")" type="text/javascript"></script>
如果您从未见过这里的jquery.showpassword-1.0.js是代码:
(function($){$.fn.extend({showPassword:function(f){return this.each(function(){var c=function(a){var a=$(a);var b=$("<input type='text' />");b.insertAfter(a).attr({'class':a.attr('class'),'style':a.attr('style')});return b};var d=function($this,$that){$that.val($this.val())};var e=function(){if($checkbox.is(':checked')){d($this,$clone);$clone.show();$this.hide()}else{d($clone,$this);$clone.hide();$this.show()}};var $clone=c(this),$this=$(this),$checkbox=$(f);$checkbox.click(function(){e()});$this.keyup(function(){d($this,$clone)});$clone.keyup(function(){d($clone,$this)});e()})}})})(jQuery);
这是一个如何工作的演示: http://sandbox.unwrongest.com/forms/jquery.showpassword.html 它只是为我做的只是在第一次单击复选框时使密码文本框消失,然后在第二次单击时重新出现,但然后在第三次单击只是密码******** disapears,不显示任何文本,但文本框保持不变。 我在.aspx视图上使用它没有问题,我如何使用Razor View?     
已邀请:
您已将
showPassword
插件附加到ID为
#text
的输入字段:
$('#text').showPassword('#checkbox');
,而您的密码字段具有
#Password
id。 所以这是一个完整的工作示例: 模型:
public class MyViewModel
{
    public string Password { get; set; }
}
控制器:
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel());
    }
}
视图:
@model AppName.Models.MyViewModel

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.showpassword-1.0.js")" type="text/javascript"></script>

<script type="text/javascript">
    $(function () {
        $('#Password').showPassword('#checkbox');
    });
</script>

<div id="passtext" class="editor-field">
    @Html.PasswordFor(m => m.Password)
    @Html.ValidationMessageFor(m => m.Password)
</div>

<label for="checkbox">
    <input id="checkbox" type="checkbox" /> Show password
</label>
    

要回复问题请先登录注册