当我们点击一个函数时,事件已经发生了很多次
$(document).one("click", "#calemonth", function() {
var clyear = $("#yearpicker").find("option:selected").text();
console.log(clyear);
$("#datepickercal").datepicker({
dateFormat: 'yy'
});
var date = new Date();
var n = date.getFullYear();
calYear = date.getFullYear();
var datArray = [];
var yearOptions1 = $("#yearpickr");
for (var i = 0; i < 5; i++) {
datArray.push(calYear);
calYear++;
}
for (var i = 0; i < datArray.length; i++) {
options += '<option value= "' + datArray[i] + '">' + datArray[i] + '</option>';
}
yearOptions1.html(options);
yearOptions1.val(clyear);
});
单击功能时需要触发一次
答案 0 :(得分:0)
您可以使用初始化为false的标志。调用该函数后,可以将其设置为true。
var clicked = false;
$(document).one("click", "#calemonth", function() {
if(!clicked)
{
var clyear = $("#yearpicker").find("option:selected").text();
console.log(clyear);
$("#datepickercal").datepicker({
dateFormat: 'yy'
});
var date = new Date();
var n = date.getFullYear();
calYear = date.getFullYear();
var datArray = [];
var yearOptions1 = $("#yearpickr");
for (var i = 0; i < 5; i++) {
datArray.push(calYear);
calYear++;
}
for (var i = 0; i < datArray.length; i++)
{
options += '<option value= "' + datArray[i] + '">' + datArray[i] +
'</option>';
}
yearOptions1.html(options);
yearOptions1.val(clyear);
});
clicked = true;
}