【发布时间】:2017-08-11 15:12:48
【问题描述】:
我正在尝试构建一个 MVC 表单,当页面加载时,电话号码的输入显示为“( ) ___-____”。我正在尝试为此使用Digital Bush's masked plug in。到目前为止,它没有在页面加载时显示。我究竟做错了什么?我已经看了好几个小时了,我看不到它。这是我到目前为止所做的:
首先,这是我的 MVC 模型中的内容:
[Required]
[Display(Name = "Home Phone Number")]
public string Buy1HomePhone { get; set; }
[Required]
[Display(Name = "Cell Phone Number")]
public string Buy1CellPhone { get; set; }
1.) 我从 Digital Bush 获取源代码并将其粘贴到标记为 maskedinput.js 的 javascript 文件中
2.) 在 maskedinput.js 中,我添加了以下代码:
$(document).ready(function () {
$('#phone').mask("(999) 999-9999");});
3.) 我在电话号码输入框中添加了以下 id 标签:
<div class="form-group">
@Html.LabelFor(model => model.Buy1HomePhone, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10" id="phone">
@Html.EditorFor(model => model.Buy1HomePhone, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Buy1HomePhone, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Buy1CellPhone, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10" id="phone">
@Html.EditorFor(model => model.Buy1CellPhone, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Buy1CellPhone, "", new { @class = "text-danger" })
</div>
</div>
4.) 我将 maskedinput.js 添加到视图中的脚本中:
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/Scripts/CarrieJQueryTest.js")
@Scripts.Render("~/Scripts/maskedinput.js")}
【问题讨论】:
标签: javascript jquery asp.net-mvc asp.net-mvc-4