【问题标题】:Bootstrap-datepicker, getDate not workingBootstrap-datepicker,getDate 不工作
【发布时间】:2014-07-30 23:05:31
【问题描述】:

我试图弄清楚如何获取用户在日期选择器上选择的日期。我已经尝试了这个网站上提到的很多东西,但它似乎不起作用。这是我的代码:

            // script for the date picker inline
            $('#calendar1 div').datepicker({
                keyboardNavigation: false,
                todayHighlight: true

                $("#calendar1 div").click( 
                var date = $("#calendar1 div").datepicker("getDate");
            });
            });

            // script for the datepicker text input
            $('#calendar2 .input-group.date').datepicker({
                keyboardNavigation: false,
                todayHighlight: true

                $("#calendar2 .input-group.date").click( 
                var date = $("#calendar2 .input-group.date").datepicker("getDate");
            });
            });

当我不运行 .click(...) 时,日期选择器看起来很好,所以我相信问题就在那里。

谢谢

【问题讨论】:

  • 您的代码无效,您应该在控制台中将其视为错误。您不能只是将事件绑定代码推入这样的对象定义中。
  • 我没用过jquery,你说的是什么控制台?
  • 浏览器控制台。您应该搜索“chrome 控制台”并阅读该页面,以及数百(数千?)的初学者 jQuery 教程中的任何一个。你的代码没有任何意义。请参阅下面的答案,这将有助于您继续前进,但完成后请删除此问题,因为它不太可能帮助其他任何人。

标签: javascript jquery html twitter-bootstrap datepicker


【解决方案1】:

你的右大括号放错了地方,你的处理程序需要是函数,像这样:

// script for the date picker inline
$('#calendar1 div').datepicker({
    keyboardNavigation: false,
    todayHighlight: true
});

$("#calendar1 div").click(function() {
    // this is the selected element
    var date = $(this).datepicker("getDate");
});

// script for the datepicker text input
$('#calendar2 .input-group.date').datepicker({
    keyboardNavigation: false,
    todayHighlight: true
});

$("#calendar2 .input-group.date").click(function() {
    // this is the selected element
    var date = $(this).datepicker("getDate");
});

【讨论】:

  • 这段代码返回:Uncaught SyntaxError: Unexpected token var (@the first var date =... declaration)
  • 看看你的代码,看看我的,再看看你的。或者,复制并粘贴我的。您缺少单击处理程序周围的 function() {...} 包装器。
  • 谢谢,我怎么能失败一个简单的 ctrl+c / ctrl+v -.-
  • 我没用过bootstrap datepicker,所以不知道。那将是一个新问题。虽然如果您的其余代码与您上面的代码类似......
【解决方案2】:

我的开始日期和结束日期如下所示:

<div class="input-daterange dateselector"
                            id="publishedDateSelector">
    <input type="text" class="input-small form-control" /><input
                                type="text" class="input-small form-control" />
</div>

以下代码初始化日期选择器:

$('.dateselector').datepicker({
    clearBtn : true,
    orientation : "top",
    todayHighlight : true
});

当我试图通过'#publishedDateSelector').datepicker('getDate') 获取日期时,它曾经返回一个不是日期表示的 jQuery 对象。我得到日期的方式是:

'#publishedDateSelector input:first').datepicker('getDate'); //StartDate
'#publishedDateSelector input:last').datepicker('getDate');  //EndDate

【讨论】:

    猜你喜欢
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    相关资源
    最近更新 更多