如何写一个显示年份的下拉框?急!!!急!!!急!!!
只是需要一个下拉框,里面只显示年份(例如:1953-2053)。应该如何做?应该怎么实现啊???非常谢谢各位朋友们能给我一个例子!!!
------解决方案--------------------做了个例子,希望对lz有帮助(IE测试通过):
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> newpage </title>
<script>
function fillSelect(){
var obj=document.getElementById( "s1 ");
for(var i=1953;i <2054;i++){
var op=new Option(i,i);
obj.add(op);
}
}
</script>
</head>
<body>
<select id=s1> </select>
<input type=button value= "填充select " onclick= "fillSelect() ">
</body>
</html>
------解决方案--------------------这个……
其实很简单啊!
不知道你是用什么框架来写,我就说个思路吧!
首先你得在后台得到一个1953-2053 数组!
比如:
Date date=new Date();
int endYear=2053;
int beginYear=1953;
ArrayList list=new ArrayList();
for(int i=beginYear;i <=endYear;i++){
Year year=new Year();
year.setYear(i);
list.add(year);
}
request.setAttribute( "dateList ", list);
request.getRequestDispatcher( "/你要显示这个下拉框的页面.jsp ").forward(request, response);
然后在前台迭代出这个数组就行了啊!
前台代码
<select name= "choseeYear ">
<c:forEach var= "year " items= "${dateList} ">
<option value= " <c:out value= "${year.year} "/> "> <c:out value= "${year.year} "/> </option>
</c:forEach>
</select>
year 是定义的一个类
private int year;
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
这样清楚了么?
------解决方案-------------------- <script>
now = new Date()
function writeYears(obj, date)
{
var years = date.getYear()+10
for (var i=date.getYear(); i <years; i++)
{
var opt = document.createElement( "option ")
opt.text = opt.value = i
obj.add(opt)
}
}
function writeMonths(obj)
{
for (var i=1; i <13; i++)
{
var opt = document.createElement( "option ")
opt.text = opt.value = Math.floor(i/10) ? i : "0 "+i
obj.add(opt)
}
}
function writeDate(obj, year, month)
{
var date = new Date( " "+year+ "/ "+(parseInt(month)+1)+ "/ "+0).getDate()
var selectedIndex = (date-1) <obj.selectedIndex ? (date-1) : obj.selectedIndex
var len = obj.options.length
if (date> len)
for (var i=len+1; i <=date; i++)
{
var opt = document.createElement( "option ")
opt.text = opt.value = Math.floor(i/10) ? i : "0 "+i
obj.add(opt)
}
else
for (var i=date; i <len; i++)
{
obj.remove(obj.length-1)
}
obj.selectedIndex = selectedIndex
}
function changeDate()
{
var year = eval( "document.forms[0].year ")
var month = eval( "document.forms[0].month ")
var date = eval( "document.forms[0].date ")
writeDate(date, year.options(year.selectedIndex).value, month.options(month.selecte