如何写一个显示年份的下拉框?急!!!急!!!急!!!
只是需要一个下拉框,里面只显示年份(例如: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.selectedIndex).value)
}
</script>
<form>
<select name= "year " onchange= "changeDate() ">
</select>
年
<select name= "month " onchange= "changeDate() ">
</select>
月
<select name= "date ">
</select>
日 </nobr> </td>
<script language= "javascript " type= "text/javascript ">
writeYears(document.forms[0].year, now)
document.forms[0].year.selectedIndex = 0
writeMonths(document.forms[0].month)
document.forms[0].month.selectedIndex = now.getMonth()
writeDate(document.forms[0].date, now.getYear(), now.getMonth()+1)
document.forms[0].date.selectedIndex = now.getDate()-1
</script>
</form>
[解决办法]
用calendar好些
[解决办法]
上网找一个日期选择器,导入后一条语句搞定。
[解决办法]
上网找一个日期选择器,导入后一条语句搞定。t00.
[解决办法]
用js
[解决办法]
这个其实很好解决啊,首先得到当前年份,然后根据业务需要得到当前年份的前几年或几十,再得到当前年份的后几年或几十年,然后再在页面填充到下拉框不就得了,这样你的下拉框中的年份不就不是静态了啦,而是随时间变化而变化的
[解决办法]
借上面兄弟的代码改了一下
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> newpage </title>
</head>
<body>
<select id=s1> </select>
<script>
var obj=document.getElementById( "s1 ");
for(var i=1953;i <2054;i++){
var op=new Option(i,i);
obj.add(op);
}
</script>
</body>
</html>
其实就是在页面最后执行一下script而已