在 MVC Razor 视图中实现 Jquery.ShowPassword
对我来说发生的一切是密码框消失并在选中复选框时重新出现。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已将
showPassword
插件附加到 ID 为#text
的输入字段:$('#text').showPassword('#checkbox'); 而您的密码字段具有
#Password
id。这是一个完整的工作示例:
模型:
控制器:
视图:
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:
Controller:
View: