【问题标题】:Get no of days in selected month in jQuery UI datepicker在 jQuery UI datepicker 中获取所选月份的天数
【发布时间】:2018-11-12 12:02:51
【问题描述】:

我正在使用 JqueryUI 日期选择器。我想计算所选月份的天数并在文本框中显示。

$(function() {
    $('.date-picker').datepicker( {
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        dateFormat: 'MM yy',
        onClose: function(dateText, inst) { 
            $(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, 1));
        }
    });
});
.ui-datepicker-calendar {
    display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/jquery-ui.css" rel="stylesheet"/>


<label for="startDate">Date :</label>
<input name="startDate" id="startDate" class="date-picker" />
<input name="noofdays" id="noofdays" />

【问题讨论】:

标签: javascript jquery jquery-ui datepicker


【解决方案1】:

onChangeMonthYear 回调在日期选择器中更改月份或年份时使用。 For reference

另外,在包含 Jquery 之后包含 Jquery UI。

$('.date-picker').datepicker({
    changeMonth: true,
    changeYear: true,
    showButtonPanel: true,
    dateFormat: 'MM yy',
    onClose: function(dateText, inst){
        var year = inst.selectedYear, month = inst.selectedMonth+1;
        $(this).datepicker('setDate', new Date(year, month, 0));
        $("#noofdays").val(new Date(year, month, 0).getDate())
    },
    onChangeMonthYear:function(year, month, date){
        $("#noofdays").val(new Date(year, month,0).getDate());
    }
});
.ui-datepicker-calendar {
    display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/jquery-ui.css" rel="stylesheet"/>

<label for="startDate">Date :</label>
<input name="startDate" id="startDate" class="date-picker" />
<input name="noofdays" id="noofdays" />

【讨论】:

猜你喜欢
  • 2014-05-22
  • 1970-01-01
  • 2017-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-31
  • 2013-02-27
  • 1970-01-01
相关资源
最近更新 更多