你可以从输入中得到一个明确的日期,但你仍然需要检查它,所以用户不会给你 4 月 31 日。
<fieldset id= "localdate">
<select id= "monthselect" size= "1">
<option selected= "selected"> January</option>
<option> February</option>
<option> March</option>
<option> April</option>
<option> May</option>
<option> June</option>
<option> July</option>
<option> August</option>
<option> September</option>
<option> October</option>
<option> November</option>
<option> December</option>
</select>
<label> Date: <input name= "inputdate" size= "2" value= "1"> </label>
<label> Year: <input name= "inputyear" size= "4" value= "2010"> </label>
</fieldset>
document.getElementsByName 的快捷方式
function byName(s, n){ n= n || 0; 返回 document.getElementsByName(s)[n]; }
function getDateInput(){
var day, y= parseInt(byName('inputyear').value),
m= byName('monthselect').selectedIndex,
d= parseInt(byName('inputdate').value);
if(!y || y<1000 || y> 3000) throw 'Bad Year '+y;
if((!d || d<1 || d> 32) throw 'Bad Date '+d;
day= new Date(y,m,d);
if(day.getDate()!= d) throw 'Bad Date '+d;
value= day.format_mysql();
}
您可以预设字段以反映当前的加载日期
onload= function(){
var now= new Date();
byName('inputyear').value= now.getFullYear();
byName('monthselect').selectedIndex= now.getMonth();
byName('inputdate').value= now.getDate();
}