2015-12-24 40 views
1

我的错误:jQuery的日期选择错误不是一个函数

Uncaught TypeError: $(...).datepicker is not a function 

我把jQuery的参考束在头顶部分的布局,以确保它得到之前javascript函数加载。我做了视图源,并没有看到大于1个参照jquery的。 jquery版本是2.1.4。任何想法发生了什么?

我的观点:

<script type="text/javascript"> 
     $(function() { 
      $('.date-picker').datepicker({ 
       changeMonth: true, 
       changeYear: true, 
       showButtonPanel: true, 
       dateFormat: 'MM yy', 
       onClose: function(dateText, inst) { 
        var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val(); 
        var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val(); 
        $(this).datepicker('setDate', new Date(year, month, 1)); 
       } 
      }); 
     }); 
</script> 

<div class="form-group"> 
      @Html.LabelFor(model => model.CCExpiration, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.CCExpiration, new { htmlAttributes = new { @class = "form-control date-picker" } }) 
       @Html.ValidationMessageFor(model => model.CCExpiration, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

_layout

<head> 
    <meta charset="utf-8" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <title>@ViewBag.Title - Vibe Productions Corporation</title> 
    @Styles.Render("~/Content/css") 
    @Scripts.Render("~/bundles/modernizr") 
    @Scripts.Render("~/bundles/jquery") 
    @Scripts.Render("~/bundles/bootstrap") 

</head> 

Bundles.cs

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
         "~/Scripts/jquery-{version}.js", 
         "~/Scripts/jquery-ui-1.11.4/jquery-{version}.js")); 
+0

编辑,以显示我的包。但是,是的,我已经包括jQuery用户界面以及 – tshoemake

+0

我被你用的OnClose函数做什么困惑。在日期选择器已经把选定的日期在文本框中,为什么要那样做动作? – Bindrid

+0

@Bindrid,如果我没有的OnClose功能选择的日期不文本框里面保存了我 – tshoemake

回答

2

目前你只是引用jQuery库在jQuery UI的包

"~/Scripts/jquery-ui-1.11.4/jquery-{version}.js")); 

这应该是:

"~/Scripts/jquery-ui-1.11.4/jquery-ui.js")); 
1

我在IE,火狐跑这并没有问题优势。我发现你的榜样的来源和唯一想我看到不同的是,几个月的日子被隐藏,所以我补充一点,我的代码。

<!DOCTYPE html> 
    <html> 
    <head> 
     <title></title> 
     <meta charset="utf-8" /> 
     <link href="Scripts/jquery-ui.css" type="text/css" rel="stylesheet" /> 
     <link href="Scripts/jquery-ui.theme.css" type="text/css" rel="stylesheet" /> 
     <script src="Scripts/jquery-2.1.4.js" type="text/javascript"></script> 
     <script src="Scripts/jquery-ui.js" type="text/javascript"></script> 
     <script type="text/javascript"> 


      $(function() { 
       $('.date-picker').datepicker({ 
        changeMonth: true, 
        changeYear: true, 
        showButtonPanel: true, 
        dateFormat: 'MM yy', 
        onClose: function (dateText, inst) { 
         var p = currentDate = $(".date-picker").datepicker("getDate"); 

         var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val(); 
         var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val(); 
         $(this).datepicker('setDate', new Date(year, month, 1)); 
        } 
       }); 
      }); 

     </script> 
     <style type="text/css"> 
      .ui-datepicker-calendar { 
       display: none; 
      } 
     </style> 
    </head> 
     <body> 
      <div id="info"> 
       <input class="date-picker" /> 
      </div> 
     </body>   
    </html> 
相关问题