var categories = {
"None":[{value:'1', text:'No category selected'}],
"Monthly":[{value:'2', text:'January'},{value:'3', text:'February'},{value:'4', text:'March'},{value:'5', text:'April'},{value:'6', text:'May'},{value:'7', text:'June'},{value:'8', text:'July'},{value:'9', text:'August'},{value:'10', text:'September'},{value:'11', text:'October'},{value:'12', text:'November'},{value:'13', text:'December'}],
"Yearly":[{value:'14', text:'2016'},{value:'15', text:'2017'},{value:'16', text:'2018'}],
}
{value:'16', text:'2018'}
하나씩 추가하는 대신 '연간'에 루프를 가질 수 있습니까? 아니면 연도를 반복하고 현재 연도까지 중지 한 다음 콤보 상자에 텍스트를 표시하십시오.
의견이있는 내 제안은 다음과 같습니다.
var categories = {
"None": [{value: '1', text: 'No category selected'}],
"Monthly": [{
value: '2',
text: 'January'
},
{
value: '3',
text: 'February'
},
{
value: '4',
text: 'March'
},
{
value: '5',
text: 'April'
},
{
value: '6',
text: 'May'
},
{
value: '7',
text: 'June'
},
{
value: '8',
text: 'July'
},
{
value: '9',
text: 'August'
},
{
value: '10',
text: 'September'
},
{
value: '11',
text: 'October'
},
{
value: '12',
text: 'November'
},
{
value: '13',
text: 'December'
}],
"Yearly": []
};
function addYearlyCategoriesStartedFromGivenYearAndValue(startYear, startValue) {
var currentYear = new Date().getFullYear();
if (currentYear < startYear) {
return; //exit to avoid endless loop
}
// assert that Yearly is array if not will set empty array
categories.Yearly = categories.Yearly || [];
// below loop will push objects to array
// starting from given startValue and startYear
// until reaches currentYear (it will also be pushed)
while (startYear <= currentYear) {
categories.Yearly.push({
// startValue++ post incrementation returns current value
// and increments this variable for next get
value: startValue++,
text: startYear++
});
}
}
// create categories.Yearly array values
addYearlyCategoriesStartedFromGivenYearAndValue(2016, 14);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다