【问题标题】:jQuery Syntax on formatting date格式化日期的jQuery语法
【发布时间】:2018-10-08 20:01:40
【问题描述】:

我正在使用 jQuery datepicker 将日期作为参数附加到 url 的末尾。一切正常,但我想将日期格式化如下:

$('#datepicker').datepicker({inline: true, dateFormat: 'ddmmyy'});

当我在“onSelect”下方代码的第 7 行尝试这个时,'onSelect' 不起作用并且只是填充输入字段,我显然得到了语法或括号或什么错误?

<html lang="en">
<head>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></scrip>
<script>
$(document).ready(function() {       
$('#datepicker').datepicker({ <!-- The issue is here -->
onSelect : function (dateText, inst) {   
var pathname = window.location.pathname;
var datepickedup = $("input#datepicker").val();
window.location.href = (pathname + "?startdate=" + datepickedup);
('#myform').submit();
}
});   
});
</script>
</head>

<body>
<form id='myform' method="post">
<input type="text" id="datepicker"/>
</form>
</body>
</html>

【问题讨论】:

    标签: jquery syntax


    【解决方案1】:

    您应该在一对括号内添加所有选项。

    $(document).ready(function () {
            $('#datepicker').datepicker({
                inline: true,
                dateFormat: 'ddmmyy',
                onSelect: function (dateText, inst) {
                    var pathname = window.location.pathname;
                    var datepickedup = $("input#datepicker").val();
                    window.location.href = (pathname + "?startdate=" + datepickedup);
                    ('#myform').submit();
                }
            });
        });
    

    【讨论】:

    • 解释您所做的更改可能会有所帮助
    • you should add all options inside of a pair of brackets. 它已经在一对括号内了。
    • @KevinB 他将第 7 行的 "$('#datepicker').datepicker({ " 更改为 "$('#datepicker').datepicker({inline: true, dateFormat: 'ddmmyy '});" 因此,onSelect 不再包含在选项中。
    • @ErcanPeker 什么?在这两种情况下,onSelect 都包含在选项中...它所属的位置...我看到的与原始版本的唯一区别是删除了 html 注释。
    • 在更改他的第 7 行代码后,它可能看起来像这样:codepen.io/peker-ercan/pen/mzRVpe。如您所见,onSelect 不会出现在选项括号中。
    猜你喜欢
    • 2011-09-20
    • 1970-01-01
    • 2013-10-20
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多