有沒有一種方法可以在選擇了年和月的時候自動獲得日期,而不用點擊完成按鈕?
。$(function() {
$('.date-picker, .to').datepicker({
changeMonth: true,
changeYear: true,
showButtonPanel: true,
dateFormat: 'yy-mm-dd'。
onClose: function(dateText, inst) {
$(this).datepicker('setDate', new Date(inst. selectedYear, inst.selectedMonth, 1))。)
$('#to').datepicker('setDate', new Date(inst. selectedYear, inst.selectedMonth 1, 0)。)
}
});
});
.ui-dateepicker-calendar {
display: none;
}
<script src="https://cdnjs. cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>/span>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min. js" integrity="sha512- uto9mlQzrs59VwILcLiRYeLKPPbS/bT71da/OEBYEwcdNUk8jYIy D176RYoop1Da f9mvkYrmj5MCLZWEtQuA=="/span> crossorigin="anonymous" referrerpolicy="no-referrer"> </script>>
<label for="startDate">年/月。 </label>
< input name="startDate"/span> id="startDate" class="date-picker" />
< input name="startDate" id="to" class="to" />
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
請看下面的例子。
。$(function() {
function setDate(target, year, month, string){
var myDate;
if ($(target).is("#first"/span>) {
day = 1;
myDate = $.datepicker.parseDate("yy-mm-dd", year "-" month "-01") 。
} else {
myDate = new Date(year, month, 0)。
}
if (string) {
$(target).val($.datepicker.formatDate("yy-mm-dd"/span>, myDate))。
} else {
$(target).datepicker("setDate", myDate) 。
}
}
$('.date-picker').datepicker({
changeMonth: true,
changeYear: true,
showButtonPanel: true,
dateFormat: 'yy-mm-dd'。
onChangeMonthYear: function(yy, mm) {
setDate(this, yy, mm, true)。
},
onClose: function(dateText, inst) {
if ($(this). is("#first") {
setDate("#last", inst.selectedYear, inst. selectedMonth 1, true)。)
}
}
});
});
.ui-dateepicker-calendar {
display: none;
}
< link rel="styleheet" href="/code. jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code. jquery.com/jquery-3.3.1.js"></script>
<script src="https://code. jquery.com/ui/1.12.1/jquery-ui.js"></script>
<label for="startDate">年/月。 </label>
< input id="first" class="date-picker" />/span>
< input id="last" class="date-picker"/>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
如果用戶改變了他們想要的月份和年份,但隨后點擊離開,這將填充該欄位,與您在onClose中的做法相同。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/319900.html
標籤:
上一篇:創建資料模型的正確方法是什么?
