日期:2014-05-17  浏览次数:20560 次

如何实现下拉框动态绑定年份????
如题,如何在下拉框中动态绑定年份,也就是绑定从一定年份到今年这个范围。例如:1990-2009,起始年份可以固定,但是终止年份必须是动态改变的。希望高手帮帮忙!先谢了!

------解决方案--------------------
只能用JS实现了,对应了IE,FF,GOOGLE,其它的没测试,结束年请修改红色部分,也可以绑定。
HTML code

<html>
<head>
<script type="text/javascript">
    var beginYear = 1990;
    function checkIE() {
        var name = navigator.appName;
        alert(name);
        if (name == "Microsoft Internet Explorer") return 1;
        else if (name == "Netscape") return 2;
    }
    function setIEValue() {
        var value = document.getElementById('value').value;
        for (i = beginYear; i <= value; i++) {
            var op = document.createElement("option");
            op.text = i;
            op.value = i;
            document.getElementById('year').add(op);
        }
    }
    function setFFValue() {
        var value = document.getElementById('value').value;
        var out = '';
        for (i = beginYear; i <= value; i++) {
            out += ('<option>' + i + '</option>');
        }
        document.getElementById('year').innerHTML = out;
    }
    var flg = checkIE();
    if (flg == 1) window.onload = setIEValue;
    else if (flg == 2) window.onload = setFFValue;
</script>
</head>
<body>
    <select id="year"></select>
<input id="value" type="hidden" value="[color=#FF0000]2005[/color]" />

</body>
</html>

------解决方案--------------------
SQL code

go
create proc GetYear
(
@StartYear int
)
as
begin
    declare @table table ([Year] int)
    while (@StartYear<=year(getdate()))
        begin
            insert into @table select @StartYear
            set @StartYear=@StartYear+1;
        end
    select * from @table
end
/*测试*/
exec GetYear 1990 

/*结果
Year
-----------
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
*/