从Bootstrap-datetimepicker无法获取所选日期的值

问题描述 投票:-1回答:1

我需要在日历结束后获得我的input中显示的值。

下面是我尝试过的代码,但从来没有点过任何一个

    $('#startDate').click(function () {
       console.log('1: ' + $('#startDate').val())
    })

    $('#startDate').change(function () {
       console.log('1.25: ' + $('#startDate').val())
    })

    $('#startDate').on("change", function() {
        alert($(this).val()); 
    });

    $('#startDate').on("change paste keyup", function() {
        alert($(this).val()); 
    });

    $("#startDate").on("input propertychange",function(){
        alert($(this).val());
    });

    $("#startDate").on("change paste keyup select", function() {
        alert($(this).val());
    });

    $("#startDate").bind('change', function(event) {
        alert( $("#startDate").attr("value") );
    });

这是我的HTML

<div class="col-md-3 col-lg-2 col-sm-12 col-xs-12">
<label for="startDate">Start date (MM/YYYY)</label>
<div class="input-group datetime" id="start_picker"
    data-date-format="MM/YYYY">
    <input type="text" name="startDate" id="startDate"
        class="form-control make-datepicker" data-date-format="MM/YYYY"
        data-parsley-date="" maxlength="7" autocomplete="off">
    <span class="input-group-addon">
        <span class="fa fa-calendar"></span>
    </span>
</div>

我也在onchange元素(input)上尝试了<input id="startDate" onchange="test()">,它应该在我的JQuery中调用函数调用,但仍然没有。

我需要这个值,所以我可以过滤一个表,但我现在难倒了。

当我第一次点击该字段以显示日历时,此行确实有效

    $('#startDate').click(function () {
        console.log('1: ' + $('#startDate').val())
    })

enter image description here

jquery html bootstrap-datepicker bootstrap-datetimepicker
1个回答
2
投票

请在尝试此代码后包含片刻js:

$('#startDate').on('changeDate', function(ev){

    console.log(moment(ev.date).format('YYYY-MM-DD'));

});

要么

$('#startDate').datepicker().on('changeDate', function(e) {

    console.log(moment(e.date).format('YYYY-MM-DD'));

});
© www.soinside.com 2019 - 2024. All rights reserved.