欢迎光临
我们一直在努力

Dynamic Date Drop-down Boxes

建站超值云服务器,限时71元/月

by kevin perkins

article

article:

one thing ive learned over the last few years is to use techniques in my applications that make it easy to debug and maintain, especially if you leave the project and have to come back to it at a later time.

besides error handling and commenting, i try to automate as much as possible to make maintenance a cake-walk. one area that can be tremendously automated is implementing date structures in your application.

for instance… if youre building an ecommerce application, youll eventually have to deal with credit card expiry. the predominant technique for handling expiration dates is to use two drop-down boxes representing the month and the year. having said that, its very easy to hard-code those dates in each drop-down box…. 1-12 for month, but what to what for the year? do you use the current year (2000) and the following two (2001,2002) ?

what happens when were in year 2001? youll have an older entry for the previous year (2000), and youll be one short for the future (2002,?). this is bad practice because your application should be smart enough to remove previous years that are invalid to present-day transactions, as well as account for new credit cards that might have an expiration date farther in the future.

to work with drop-down dates dynamically, i determine the current month, current year, and how far into the future i want to account for expiration. my rule is 5 years, but each application is different:

<%@language=vbscript%>

<%

———————————————————————————-

you could use similar looping techniques with javascript

———————————————————————————-

%>

<html>

<head></head>

<body>

<form>

<%

———————————————————————————-

build the month drop-down box

———————————————————————————-

imonth = month(now())

this produces the current months integer (ex. 7)

%>

<select name="drpmonth">

<% for i = 1 to 12 %>

<option <%

this will select the current month in the list

if cint(imonth = i) then

response.write "selected "

end if

%>value="<%= i %>"><%= i %></option>

<% next %>

</select>

<!– drop-down separator –>

/

<!– end separator –>

<%

———————————————————————————-

build the year drop-down box

———————————————————————————-

iyear = year(now())

this produces the current years integer (ex. 2000)

%>

<select name="drpyear">

<% for i = iyear to ( cint(iyear + 5) ) %>

<option <%

this will select the current year in the list

if cint(iyear = i) then

response.write "selected "

end if

%>value="<%= i %>"><%= i %></option>

<% next %>

</select>

</form>

</body>

</html>

of course, if you have more complex date interactions, you can manipulate the if/then statement inside of the loop to select the appropriate value. and, if youre working with dates in the past, you can reindex the starting value of (i) by subtracting how many years you want to go back…

example:

"for i = iyear to …" tells the application to do something "from 2000 to …"

"for i = cint(iyear – 2) to …" tells the application to do something "from 1998 to … "

-end-

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Dynamic Date Drop-down Boxes
分享到: 更多 (0)