$("select").change(function() {
//var date = "2017-04-06"
//var date = "2017-01-06"
$(".appendhere").empty();
var date = $("option:selected",this).text();
var monthnumber = date.split("-")[1] - 1;
var yearnumber = date.split("-")[0];
var months = 12;
var m_names = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
for (var i = months - 1; i >= 0; i--) {
var div = $('<div class="row" style="margin-top:6px;"><div class="col-md-6"><label class="col-md-6 col-form-label">' + yearnumber + " " + m_names[monthnumber] + '</label></div>');
$(".appendhere").append(div)
monthnumber == 11 ? monthnumber = 0 : monthnumber++;
m_names[monthnumber] == "December" ? yearnumber++ : yearnumber + 0;
}
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option>2017-04-06</option>
<option>2017-01-06</option>
</select>
<div class="appendhere"></div>
在选择更改时我想根据选择日期追加月份。
我可以添加月份和年份,但问题是12月的年份总是提前1年。
我知道这是小细节,但我无法指出我做错了什么。
答案 0 :(得分:1)
反过来这两行:
monthnumber == 11 ? monthnumber = 0 : monthnumber++;
m_names[monthnumber] == "December" ? yearnumber++ : yearnumber + 0;
您目前正在之前增加月份,以测试年份是否应该更改。
$("select").change(function() {
//var date = "2017-04-06"
//var date = "2017-01-06"
$(".appendhere").empty();
var date = $("option:selected",this).text();
var monthnumber = date.split("-")[1] - 1;
var yearnumber = date.split("-")[0];
var months = 12;
var m_names = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
for (var i = months - 1; i >= 0; i--) {
var div = $('<div class="row" style="margin-top:6px;"><div class="col-md-6"><label class="col-md-6 col-form-label">' + yearnumber + " " + m_names[monthnumber] + '</label></div>');
$(".appendhere").append(div)
m_names[monthnumber] == "December" ? yearnumber++ : yearnumber + 0;
monthnumber == 11 ? monthnumber = 0 : monthnumber++;
}
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option>2017-04-06</option>
<option>2017-01-06</option>
</select>
<div class="appendhere"></div>
答案 1 :(得分:1)
在条件运算符中替换"January"
"December"
。
m_names[monthnumber] == "January" ? yearnumber++ : yearnumber + 0;
答案 2 :(得分:0)
你在错误的地方增加了一年。现在检查此输出。
$("select").change(function() {
//var date = "2017-04-06"
//var date = "2017-01-06"
$(".appendhere").empty();
var date = $("option:selected", this).text();
var monthnumber = date.split("-")[1] - 1;
var yearnumber = date.split("-")[0];
var months = 12;
var m_names = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
for (var i = months - 1; i >= 0; i--) {
;
var div = $('<div class="row" style="margin-top:6px;"><div class="col-md-6"><label class="col-md-6 col-form-label">' + ( m_names[monthnumber] == "December" ? yearnumber++ : yearnumber) + " " + m_names[monthnumber] + '</label></div>');
$(".appendhere").append(div)
monthnumber == 11 ? monthnumber = 0 : monthnumber++;
}
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option>2017-04-06</option>
<option>2017-01-06</option>
</select>
<div class="appendhere"></div>
答案 3 :(得分:0)
因为您在检查其名称monthnumber
之前已增加m_names[monthnumber] == "December" ?
。
您应该在代码行下面更改2的顺序:
自:
monthnumber == 11 ? monthnumber = 0 : monthnumber++;
m_names[monthnumber] == "December" ? yearnumber++ : yearnumber + 0;
要:
m_names[monthnumber] == "December" ? yearnumber++ : yearnumber + 0;
monthnumber == 11 ? monthnumber = 0 : monthnumber++;
$("select").change(function() {
//var date = "2017-04-06"
//var date = "2017-01-06"
$(".appendhere").empty();
var date = $("option:selected",this).text();
var monthnumber = date.split("-")[1] - 1;
var yearnumber = date.split("-")[0];
var months = 12;
var m_names = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
for (var i = months - 1; i >= 0; i--) {
var div = $('<div class="row" style="margin-top:6px;"><div class="col-md-6"><label class="col-md-6 col-form-label">' + yearnumber + " " + m_names[monthnumber] + '</label></div>');
$(".appendhere").append(div)
m_names[monthnumber] == "December" ? yearnumber++ : yearnumber + 0;
monthnumber == 11 ? monthnumber = 0 : monthnumber++;
}
}).change();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option>2017-04-06</option>
<option>2017-01-06</option>
</select>
<div class="appendhere"></div>
&#13;
答案 4 :(得分:0)
反转最后两行dude
monthnumber == 11 ? monthnumber = 0 : monthnumber++;
m_names[monthnumber] == "December" ? yearnumber++ : yearnumber + 0;
答案 5 :(得分:0)
希望这会奏效。
for (var i = months -1; i >= 0; i--) {
monthnumber == 11 ? monthnumber = 0 : monthnumber++;
var div = $('<div class="row" style="margin-top:6px;"><div class="col-md-6"><label class="col-md-6 col-form-label">' + yearnumber + " " + m_names[monthnumber] + '</label></div>');
$(".appendhere").append(div)
m_names[monthnumber] == "December" ? yearnumber++ : yearnumber + 0;
}