代码之家  ›  专栏  ›  技术社区  ›  w0051977

在决定输入字段的类型之前,先检查浏览器是否符合HTML5

  •  2
  • w0051977  · 技术社区  · 6 年前

    假设我有一些JavaScript代码如下所示:

     function CheckDateField()
        {
        if (!Modernizr.inputtypes.date) {
            $(function () {
                $(".datefield").datepicker();
            });
        }
        }
    

    <div class="form-group">
                    <label asp-for="DOB" class="control-label"></label>
                    @*@Html.TextBox("datepicker")*@
                    @Html.TextBox("datepicker", null, new { @readonly = "readonly" })
    

    如何使用JavaScript中的字段?我试过这个:

    <label asp-for="DOB" class="control-label" onload="CheckDateField()"></label>
    

    如果可能的话,最好将JavaScript直接放在lavel中。我花了两个小时在谷歌上搜索这个,并尝试了一些东西,但都没有成功。

    1 回复  |  直到 6 年前
        1
  •  3
  •   emerson.marini    6 年前

    只需检查 HTML5 input date 当页面被加载并相应地操作时的特性。 input 没有课 datefield 设置。

    生成的标记类似于:

    <label for="dob">Date of birth</label>
    <input type="date" name="dob" id="dob" class="datefield">
    

    onload 事件处理程序和 input type date 首先。

    你的脚本处理 datepicker 应类似于:

    // When the document is ready.
    $(function () {
        // If input type date isn't supported.
        if (!Modernizr.inputtypes.date) {
            var el = $(".datefield");
    
            // In a single call (chained) set the input type to text
            // and attach the datepicker.
            el.prop("type", "text").datepicker();
        }
    });
    

    Demo