jQuery datepicker在cshtml页面中不起作用

时间:2017-05-21 14:13:27

标签: javascript jquery jquery-ui razor datepicker

我正在尝试在jQuery中实现一个简单的datepicker,但是我收到的错误就像'datepicker not a function'。真的不明白为什么,也不知道我犯了什么错误。我还在捆绑包中添加了jQuery,并且在_Layout页面中呈现了bundle脚本,并且脚本在视图中添加但没有运气。任何人都可以帮助解决问题所在。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Reports</title>
<link href="~/Content/Site.css" rel="stylesheet" />
<link href="~/Content/themes/base/datepicker.css" rel="stylesheet" />
<link href="~/Content/themes/ui-lightness/jquery-ui.ui-lightness.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.12.4.js"></script>
<script src="~/Scripts/jquery-ui-1.12.1.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $(function () {
            var j = $.noConflict(true);
            $("#fromdate").datepicker();
        });
    });
    </script>
</head>
<body>
<div class="container">
    <!-- Form Starts-->
    <form name="PTTReport" style="padding-top:20px">
        <div class="panel panel-primary">
            <div style="padding-bottom:25px" class="panel-heading fixed_panel">
                    <span>Report Filters</span>
            </div>
            <div class="panel-body">
                <div class="row">
                    @*From*@
                    <label class="col-sm-1">From</label>
                    <div class="col-sm-2">
                        <div class="input-group">
                            <input type="text" class="form-control" id="fromdate">
                            <span class="input-group-btn">
                                <button class="btn btn-default" type="button">
                                    <span class="glyphicon glyphicon-calendar"></span>
                                </button>
                            </span>
                        </div><!-- /input-group -->
                    </div>

                    @*To*@
                    <label class="col-sm-1">To</label>
                    <div class="col-sm-2">
                        <div class="input-group">
                            <input type="text" class="form-control">
                            <span class="input-group-btn">
                                <button class="btn btn-default" type="button">
                                    <span class="glyphicon glyphicon-calendar"></span>
                                </button>
                            </span>
                        </div><!-- /input-group -->
                    </div>
                </div>
                <!--Submit Button-->
                <div class="col-sm-10"></div>
                <div class="col-sm-2">
                    <button type="submit" class="btn btn-primary text-center">View Projects</button>
                </div>
            </div>
        </div>

3 个答案:

答案 0 :(得分:1)

控制台错误清楚地表明它无法找到文件路径。

试试这个:

jQuery .css和.js文件来源

<link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
     rel = "stylesheet">
  <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
  <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

JS

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

HTML正文

<body> 
  <p>Enter Date: <input type = "text" id = "datepicker"></p>
</body>

答案 1 :(得分:0)

尝试以下方法:

var $j = jQuery.noConflict();
$j("#fromdate").datepicker();

答案 2 :(得分:0)

请务必检查您的浏览器控制台和网络电话,以确定是否首先加载所有必需的文件 $。datepicker()不是函数。 它清楚地表明它无法识别定义此函数的基本脚本。使用来定义相对路径肯定不会加载你的jquery / jquery-ui脚本。如果你想确保你可以在这里检查这个代码,它可以工作!

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Reports</title>
  <script
  src="https://code.jquery.com/jquery-2.2.4.min.js"
  integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
  crossorigin="anonymous"></script>
  <script
  src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"
  integrity="sha256-xNjb53/rY+WmG+4L6tTl9m6PpqknWZvRt0rO1SRnJzw="
  crossorigin="anonymous"></script>
<script>

    $(document).ready(function () {
        $(function () {
            //var j = $.noConflict(true);
            $("#fromdate,#todate").datepicker();
        });
    });
    </script>
</head>
<body>
<div class="container">
    <!-- Form Starts-->
    <form name="PTTReport" style="padding-top:20px">
        <div class="panel panel-primary">
            <div style="padding-bottom:25px" class="panel-heading fixed_panel">
                    <span>Report Filters</span>
            </div>
            <div class="panel-body">
                <div class="row">
                    @*From*@
                    <label class="col-sm-1">From</label>
                    <div class="col-sm-2">
                        <div class="input-group">
                            <input type="text" class="form-control" id="fromdate">
                            <span class="input-group-btn">
                                <button class="btn btn-default" type="button">
                                    <span class="glyphicon glyphicon-calendar"></span>
                                </button>
                            </span>
                        </div><!-- /input-group -->
                    </div>

                    @*To*@
                    <label class="col-sm-1">To</label>
                    <div class="col-sm-2">
                        <div class="input-group">
                            <input type="text" class="form-control" id="todate">
                            <span class="input-group-btn">
                                <button class="btn btn-default" type="button">
                                    <span class="glyphicon glyphicon-calendar"></span>
                                </button>
                            </span>
                        </div><!-- /input-group -->
                    </div>
                </div>
                <!--Submit Button-->
                <div class="col-sm-10"></div>
                <div class="col-sm-2">
                    <button type="submit" class="btn btn-primary text-center">View Projects</button>
                </div>
            </div>
        </div>

我在cdn中添加了jquery和jquery-ui并且工作正常,确保在jquery 之后添加了jquery-ui脚本标记。 更新的JS Bin是here

下一步做什么

使用&#39; /&#39; 作为链接css和脚本的相对路径,以解决您的问题。