【问题标题】:I need another set of eyes: jQuery in MVC not working我需要另一双眼睛:MVC 中的 jQuery 不起作用
【发布时间】: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


    【解决方案1】:

    我认为您需要在 input 元素而不是 div 上初始化掩码。另外,既然你有 2 你应该把它变成一个类而不是一个 id(即&lt;div class="col-md-10" class="phone"&gt;

    像这样:

    $(document).ready(function () {
    $('.phone input').mask("(999) 999-9999");});
    

    【讨论】:

    • 这很有意义,我试了一下,它不起作用。我什至尝试将 id="phone" 翻转为 class="phone" 但这没有用。还有其他想法吗?
    • 不,原来我需要将我的 EditorFor 更改为 TextBoxFor,然后 jQuery 出现了。我不明白为什么这解决了它,但它确实。感谢您的帮助!
    【解决方案2】:

    我修好了!我不明白为什么这是修复,但它是。这是我的新代码:

    在视图中...

                 <div class="form-group">
                    @Html.LabelFor(model => model.Buy1HomePhone, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.TextBoxFor(model => model.Buy1HomePhone, new { id = "applicantHomePhone", @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">
                        @Html.TextBoxFor(model => model.Buy1CellPhone, new { id = "applicantCellPhone", @class = "form-control" })
                        @Html.ValidationMessageFor(model => model.Buy1CellPhone, "", new { @class = "text-danger" })
                    </div>
                </div>
    

    我最终将 EditorFor 更改为 TextBoxFor,然后为 jQuery 添加一个 id 以识别它。我尝试了多个课程,但 Visual Studio 并不满意。我还尝试在每个 TextBoxFor 上执行相同的 id,但这不起作用。只有第一个带有 id 标签的输入框显示了 jQuery。因此,在找到更有效的方法之前,我将为页面上的每个电话号码制作唯一的 id 标签。

    这是我的 jQuery 的样子:

    $(document).ready(function () {
        $('#applicantHomePhone').mask("(999) 999-9999");
        $('#applicantCellPhone').mask("(999) 999-9999");
    });
    

    感谢大家带领我朝着正确的方向前进!

    【讨论】:

    • 最好使用类名 - 例如new { @class = "form-control phone" } 然后是 $('.phone').mask. Other wise just use the default id` 控件的属性 - $(#Buy1CellPhone).mask(...);` 等(无需覆盖现有的 id 属性)
    【解决方案3】:

    您正在为 id 为“phone”的元素制作掩码:

    $('#phone').mask("(999) 999-9999");});
    

    但是这个元素是div

    <div class="col-md-10" id="phone">
    

    只需将掩码放在输入元素上。这是您的解决方案:

    $('#phone .form-control').mask("(999) 999-9999");});
    

    只要“form-control”是您赋予电话号码元素的类。

    你应该考虑给它一个更具体的类,以确保你不会将掩码应用于其他不需要的元素。

    【讨论】:

    • 这对我来说很有意义,但它不起作用。不幸的是,form-control 是一个来自 MVC 模板的类,所以我不能真正搞砸它。
    • 别乱来,再添一个 : , new { htmlAttributes = new { @class= "form-control MyClass" } } 然后指出 $('#phone input.MyClass ').mask("(999) 999-9999")
    • 原来我需要在视图中将 EditorFor 更改为 TextBoxFor。我不知道为什么会这样,但是一旦我这样做了,jQuery 就会像我想要的那样在输入框中弹出。非常感谢您带领我朝着正确的方向前进!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-07
    • 2014-08-03
    • 2011-01-10
    相关资源
    最近更新 更多