简体   繁体   中英

How to do something when date is selected from date picker

I am using this date picker http://bootstrap-datepicker.readthedocs.io/en/latest/events.html#hide .

The code is written as,

<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
});

I am trying to find the event when the date is selected and do something on that event but in this document, I am not able to find that. Please help!

Please Try

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

Since you are using bootstrap plugin for datepicker, you can check reference manual for it.

Trigger to changeDate event event like

$('#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.
    });

Date is stored in callback parameter e.date .

$('#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
}) ;

this code will hold everything you need

Try to below code

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

JS Code

function functionname(e){
    your logic
}

Click Here to show demo in jsFiddle

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

    // enter code here
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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