我正在使用bootstrapg datepicker,我使用以下js:
$("#sandbox-container div").datepicker({
format: "yyyy",
viewMode: "years",
minViewMode: "years"
});
$(".datepicker").on("dp.change", function(e) {
var myDate = $(this).val();
console.log(myDate);
});
和html:
<div class="row">
<div class="col">
<label class="usp-label usp-label-category" for="usp-category">Time</label>
<div id="sandbox-container"><div></div></div>
<div>
</div>
但我在控制台中没有任何价值或任何东西
更新
也尝试过:
$("#sandbox-container div").datepicker({
format: "yyyy",
viewMode: "years",
minViewMode: "years"
}).on("dp.change", function(e) {
var myDate = $(this).val();
console.log(myDate);
});
答案 0 :(得分:0)
此要求是从显示年历的日期选择器中获取点击年份的值。
尝试:
$("#sandbox-container div.datepicker").datepicker({
format: "yyyy",
viewMode: "years",
minViewMode: "years"
}).on("click", function(e) {
console.log(e.target.innerText);
});
并更改html
<div id="sandbox-container"><div class="datepicker"></div></div>
答案 1 :(得分:0)
这就是我解决它的方法:
<强> HTML 强>
<div class="col">
<label class="usp-label usp-label-category" for="usp-category">Time</label>
<div id="sandbox-container"><div></div></div>
</div>
<强> JS 强>
$(function(){
$('#sandbox-container div').datepicker({
format: "yyyy",
viewMode: "years",
minViewMode: "years"
});
$("#sandbox-container span").on("click", function(e) {
$("#sandbox-container span").removeAttr("class", "selected_year");
$(this).attr("class", "selected_year");
var myDate = $(".selected_year").text();
console.log(myDate);
});
});