我希望根据下拉列表添加月份,但我已多次尝试对其进行编码。我仍然得到一个输出。我正在使用jQuery datepicker
添加月份
$(function() {
var p = document.getElementById("period");
$(".firstcal").datepicker({
dateFormat: "dd/mm/yy",
onSelect: function(dateText, instance) {
date = $.datepicker.parseDate(instance.settings.dateFormat, dateText, instance.settings);
if(document.getElementById("onemonth").value == 1){
date.setMonth(date.getMonth() + 1);
$(".secondcal").datepicker("setDate", date);
}else
if (document.getElementById("threemonth").value == 3){
date.setMonth(date.getMonth() + 3);
$(".secondcal").datepicker("setDate", date);
}else if(document.getElementById("sixmonth").value ==6){
date.setMonth(date.getMonth() + 6);
$(".secondcal").datepicker("setDate", date);
}
}
});
$(".secondcal").datepicker({
dateFormat: "dd/mm/yy"
});
});
<label style="color: #f2f2f2">Pay Period</label> <br/>
<select id="period">
<option>Select the period</option>
<option id="onemonth"value="1">one month</option>
<option id="threemonth"value="3">three month</option>
<option id="sixmonth"value="6">six month </option> <br/>
</select>
<br/><br/>
<label style="color: #f2f2f2" >Date (From,To)</label> <br/>
<input type="text" class="firstcal"> <input type="text" class="secondcal">
答案 0 :(得分:0)
您应该检查select
标记的值而不是option
标记。将脚本更改为以下内容,我将检查id为period
的元素的值。
$(function() {
var p = document.getElementById("period");
$(".firstcal").datepicker({
dateFormat: "dd/mm/yy",
onSelect: function(dateText, instance) {
date = $.datepicker.parseDate(instance.settings.dateFormat, dateText, instance.settings);
if (document.getElementById("period").value == 1) {
date.setMonth(date.getMonth() + 1);
$(".secondcal").datepicker("setDate", date);
} else
if (document.getElementById("period").value == 3) {
date.setMonth(date.getMonth() + 3);
$(".secondcal").datepicker("setDate", date);
} else if (document.getElementById("period").value == 6) {
date.setMonth(date.getMonth() + 6);
$(".secondcal").datepicker("setDate", date);
}
}
});
$(".secondcal").datepicker({
dateFormat: "dd/mm/yy"
});
});