从日期选择器中选择日期时如何做某事

时间:2017-10-06 06:42:16

标签: javascript jquery twitter-bootstrap

我正在使用此日期选择器http://bootstrap-datepicker.readthedocs.io/en/latest/events.html#hide

代码写成,

<div class="form-group" id="purchasedatediv">
    <label class="col-sm-3 control-label">Purchase Date *</label>
    <div class="col-sm-9">
        <div class="input-group date">
            <span class="input-group-addon">
                <i class="fa fa-calendar"></i>
            </span>
            <input type="text" value="<?php if($result != '') echo date('m/d/Y', $result->purchasedate->sec); ?>"
                name="purchasedate" id="purchasedate" class="form-control required">
        </div>
    </div>
</div>

$('#purchasedatediv .input-group.date').datepicker({
    todayBtn: "linked",
    keyboardNavigation: false,
    forceParse: false,
    calendarWeeks: true,
    autoclose: true,
    format: formatToApply
});

我试图在选择日期时找到该事件并对该事件做一些事情,但在本文档中,我无法找到。请帮忙!

5 个答案:

答案 0 :(得分:2)

请尝试

$('#datepicker').datepicker().on("change", function(e) {
  console.log("Date changed: ", e.target.value);
});

答案 1 :(得分:1)

由于您正在为datepicker使用bootstrap插件,因此可以查看参考手册。

触发changeDate事件事件,如

$('#purchasedatediv .input-group.date').datepicker({
        todayBtn: "linked",
        keyboardNavigation: false,
        forceParse: false,
        calendarWeeks: true,
        autoclose: true,
        format: formatToApply
    }).on('changeDate', function(e) {
        //Print e variable
        console.log('date changed', e);
        //Do your stuff here, read date, parse it, whatever.
    });

日期存储在回调参数e.date中。

答案 2 :(得分:1)

$('#purchasedatediv .input-group.date').datepicker({
        todayBtn: "linked",
        keyboardNavigation: false,
        forceParse: false,
        calendarWeeks: true,
        autoclose: true,
        format: formatToApply
    }). on(picker_event, function(e){
        //do your stuf
}) ;

此代码将包含您需要的所有内容

答案 3 :(得分:0)

尝试以下代码

<input type="text" value=""
                name="purchasedate" id="purchasedate" class="form-control required" onChange="functionname(this)">

JS代码

function functionname(e){
    your logic
}

Click Here to show demo in jsFiddle

答案 4 :(得分:-1)

$('#purchasedate').off('change').on('change', function(){

    // enter code here
})