在 MVC Razor 视图中实现 Jquery.ShowPassword

发布于 2024-10-17 10:12:47 字数 2189 浏览 8 评论 0原文

对我来说发生的一切是密码框消失并在选中复选框时重新出现。

razor 视图中的代码:

<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>

我在底部的 @renderparttail 中有 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

它对我所做的只是使密码文本框在第一次单击复选框时消失,然后在第二次单击时重新出现,但在第三次单击时仅显示密码** ****** 消失,根本不显示任何文本,但文本框保留。

我已经在 .aspx 视图上使用它,没有任何问题,如何使用 Razor 视图?

All that is happening for me is the Password Box is just disappearing and re-appearing on when the check box is checked.

Code in razor view:

<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>

I have the Javascript function in a @renderpartail at the bottom:

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

And the jquery.showpassword-1.0.js included at the top of the view with the other scripts:

<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>

And incase you never seen the jquery.showpassword-1.0.js here is code from that:

(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);

Here is a Demo of how it is supposed to work:
http://sandbox.unwrongest.com/forms/jquery.showpassword.html

All it is doing for me is just making the password textbox disapear on the first click of the checkbox, and then reappear on the second click, but then on the Third Click just the password ******** disapears, not showing any text at all, but the textbox stays.

I have used it on .aspx views with no problem, how do I impliment using a Razor View?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻梦旅人 2024-10-24 10:12:47

您已将 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>

You have attached the showPassword plugin to an input field with id #text : $('#text').showPassword('#checkbox'); whereas your password field has the #Password id.

So here's a full working example:

Model:

public class MyViewModel
{
    public string Password { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel());
    }
}

View:

@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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文