如何从内联日期选择器中获取年份值?
How do I get year value form datepicker inline?
我正在使用 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);
});
这里的要求是从显示年历的日期选择器中获取点击年份的值。
尝试:
$("#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>
我是这样解决的:
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);
});
});
我正在使用 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);
});
这里的要求是从显示年历的日期选择器中获取点击年份的值。
尝试:
$("#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>
我是这样解决的:
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);
});
});