【问题标题】:Cannot Get Value Of Selected Date From Bootstrap-datetimepicker无法从 Bootstrap-datetimepicker 获取所选日期的值
【发布时间】:2019-03-19 10:20:39
【问题描述】:

我需要在日历关闭后获取我的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>

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

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

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

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

【问题讨论】:

    标签: jquery html bootstrap-datepicker bootstrap-datetimepicker


    【解决方案1】:

    请在尝试此代码后包含 moment 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'));
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      • 2015-11-26
      • 2018-11-21
      • 2015-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多