简体   繁体   English

使用 Struts2 JQuery 日期选择器标签获取未捕获的 TypeError 错误

[英]Getting Uncaught TypeError errors using Struts2 JQuery datepicker tag

I have a Struts 2 application with UI built using bootstrap and am trying to get JQuery datepicker working using struts2 jquery tag library.我有一个带有使用引导程序构建的 UI 的 Struts 2 应用程序,并且正在尝试使用 struts2 jquery 标记库使 JQuery 日期选择器工作。 I am using <sj:head compressed="true" jqueryui="true" /> to include JQuery CSS and JS libraries.我正在使用<sj:head compressed="true" jqueryui="true" />来包含 JQuery CSS 和 JS 库。 Below is what gets included using sj:head :以下是使用sj:head包含的内容:

<script type="text/javascript" src="/struts/js/base/jquery-1.11.0.min.js"></script>
        <script type="text/javascript" src="/struts/js/base/jquery.ui.core.min.js?s2j=3.7.1"></script>
<script type="text/javascript" src="/struts/js/plugins/jquery.subscribe.min.js?s2j=3.7.1"></script>

<script type="text/javascript" src="/struts/js/struts2/jquery.struts2.min.js?s2j=3.7.1"></script>

<script type="text/javascript">
    $(function () {
        jQuery.struts2_jquery.version = "3.7.1";
        jQuery.scriptPath = "/struts/";
        jQuery.ajaxSettings.traditional = true;

        jQuery.ajaxSetup({
            cache: false
        });

        jQuery.struts2_jquery.require("js/struts2/jquery.ui.struts2.min.js?s2j=3.7.1");

    });
</script>

    <link id="jquery_theme_link" rel="stylesheet"
          href="/struts/themes/smoothness/jquery-ui.css?s2j=3.7.1" type="text/css"/>

Below is the code related to datepicker in JSP:下面是 JSP 中与 datepicker 相关的代码:

<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<div class="form-group row">
    <label class="col-sm-2 control-label">From</label>
    <div class="col-sm-4">
        <sj:datepicker id="fromDate" displayFormat="m/d/y" showOn="focus" name="startDate"></sj:datepicker>
    </div>
    <label class="col-sm-2 control-label">To</label>
    <div class="col-sm-4">
        <sj:datepicker id="toDate" displayFormat="m/d/y" showOn="focus" name="endDate"></sj:datepicker>
    </div>
</div>

<script>
$(function() {

    $("#fromDate").datepicker({
        dateFormat: 'yy-mm-dd',
        maxDate: 0,
        changeYear: true 
    }).attr('readonly', 'readonly');

    $("#toDate").datepicker({
        dateFormat: 'yy-mm-dd',
        maxDate: 0,
        changeYear: true 
    }).attr('readonly', 'readonly');
});
</script>

The fields are getting displayed fine and seems to be working fine as well.这些字段显示得很好,似乎也工作正常。 However, I am getting the following errors in console:但是,我在控制台中收到以下错误:

Uncaught TypeError: Cannot set properties of undefined (setting 'version')
    at HTMLDocument.<anonymous> (viewResults:62:39)
    at j (jquery-1.11.0.min.js:776:111)
    at Object.fireWith [as resolveWith] (jquery-1.11.0.min.js:810:100)
    at Function.ready (jquery-1.11.0.min.js:865:65)
    at HTMLDocument.K (jquery-1.11.0.min.js:873:97)
jquery-3.1.1.min.js:2 Uncaught TypeError: Cannot read properties of undefined (reading 'initDatepicker')
    at HTMLDocument.<anonymous> (viewResults:360:27)
    at j (jquery-3.1.1.min.js:2:29948)
    at k (jquery-3.1.1.min.js:2:30262)
jquery-3.1.1.min.js:2 Uncaught TypeError: Cannot read properties of undefined (reading 'bind')
    at HTMLDocument.<anonymous> (viewResults:372:26)
    at j (jquery-3.1.1.min.js:2:29948)
    at k (jquery-3.1.1.min.js:2:30262)
jquery-3.1.1.min.js:2 Uncaught TypeError: Cannot read properties of undefined (reading 'initDatepicker')
    at HTMLDocument.<anonymous> (viewResults:382:27)
    at j (jquery-3.1.1.min.js:2:29948)
    at k (jquery-3.1.1.min.js:2:30262)
jquery-3.1.1.min.js:2 Uncaught TypeError: Cannot read properties of undefined (reading 'bind')
    at HTMLDocument.<anonymous> (viewResults:394:26)
    at j (jquery-3.1.1.min.js:2:29948)
    at k (jquery-3.1.1.min.js:2:30262)

I cannot figure out what am I getting wrong here that is causing these errors.我无法弄清楚导致这些错误的原因是什么。 I tried removing the sj:head and instead included the libraries manually but that didn't work either.我尝试删除sj:head并手动包含库,但这也不起作用。

The wrong version of jQuery framework is loaded.加载了错误版本的 jQuery 框架。

<script type="text/javascript" src="/struts/js/base/jquery-1.11.0.min.js"></script>

Don't load jQuery library twice on the same page.不要在同一页面上两次加载 jQuery 库。 First time you load it with第一次加载它

<script src="... ">

the second time you load it with第二次加载它

<sj:head/>       

You should include <sj:head/> tag in the body of the <head> tag.您应该在<head>标记的正文中包含<sj:head/>标记。

If you use the <sj:head> tag then you can customize it to use different version of jQuery.如果您使用<sj:head>标签,那么您可以自定义它以使用不同版本的 jQuery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM