動態設置select 下拉的值

//動態時間
var nyears = 20; //年份跨度,從開始往上20年

//方法:創建我們要的select下拉列表
function createSelect (begin_year) {  //begin_year : 從哪一年開始數,要數字。
	
	try{
		var select = jQuery("<select name='yzDate' id='yzDate'></select>");  //select標籤
	}
	catch(ex){
		alert(ex.description);
	}
	//開始循環
	var option;
	for(var i=0;i<=nyears;i++){   
		option = jQuery('<option></option>');
		option.html(begin_year-i);  //設置它顯示出來的項
		option.val(begin_year-i);  //設置它的值
		select.append(option);
	}
return select;
}
jQuery(document).ready(function(){ 

	var myDate = new Date();
	var year = myDate.getFullYear();
	var selectElem = createSelect(year);  // 這裏返回的是一個select的jquery對象

$("#yearTime").append(selectElem);
var yzDateval= '${yzDate}';   // 記錄上次選擇的value
var yzDate= document.form1.yzDate;   
for(var i=0;i<yzDate.options.length;i++){   
    //將上次那個選項置爲選擇狀態   
    if(yzDate.options[i].value==yzDateval){   
     yzDate.options[i].selected=true;   
        break;   
    }   
}  
}); 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章