用于Html.TextBox控件的jQuery UI Datepicker

ZVe*_*nue 2 asp.net-mvc jquery jquery-ui datepicker

我需要显示jQuery UI Datepicker插件,Html.TextBox我该怎么做?

jQuery代码:

<script>
    $(function () {
        $("#datepicker").datepicker();
    });
</script>

<div class="demo">
     <p>Date: <input type="text" id="datepicker" style = "width:150px"/></p>
</div>
Run Code Online (Sandbox Code Playgroud)

上面的代码在我的MVC视图中为我提供了一个文本框控件,我可以在其中单击控件并弹出日历控件,它运行良好,但我想要的是重现相同的行为,Html.Textbox我将在稍后使用代码,像这样:

<%= Html.TextBox("Date", "", new { style = "width:150px"})%> 
Run Code Online (Sandbox Code Playgroud)

如何让jQuery UI Datepicker插件在上面的代码行中运行?

Emm*_*l N 6

加入class="datepicker"你的htmlAttributes

  <script>
 $(function () {
    $(".datepicker").datepicker();
 });
 </script>

 <div class="demo">
     <p>Date: <input type="text" id="datepicker" style = "width:150px"/></p>
 </div>
  <%= Html.TextBox("Date", "", new { style = "width:150px", @class = "datepicker"})%>
Run Code Online (Sandbox Code Playgroud)

  • 您不应该在同一页面上有2个相同的ID. (2认同)