【发布时间】:2011-03-17 14:13:48
【问题描述】:
我正在创建一个表单,我希望它询问此人的完整生日。我正在收集他们出生的日期、月份和年份。我有两个问题:
我可以这样做吗选择框。
-
我有一个警告框,在提交之前会显示他们的信息。现在它告诉他们,出生日期:月/日/年(例如:12/6/1970)。有没有代码可以让我这样写,Age: {Age}(例如:Age: 34 岁)。
谢谢!
【问题讨论】:
标签: javascript
我正在创建一个表单,我希望它询问此人的完整生日。我正在收集他们出生的日期、月份和年份。我有两个问题:
我可以这样做吗选择框。
我有一个警告框,在提交之前会显示他们的信息。现在它告诉他们,出生日期:月/日/年(例如:12/6/1970)。有没有代码可以让我这样写,Age: {Age}(例如:Age: 34 岁)。
谢谢!
【问题讨论】:
标签: javascript
如果您使用的是 jQuery,那么您可以查看 $.datePicker,它将为您处理月/日情况。 (以及其他选项here)。至于格式化,可以看$().tmpl。
如果您只是使用纯 JavaScript,您可能可以检查月份值并根据该更新来确定该月的天数。所以像这样。
var month_select = document.getElementById("month");
month_select.onchange = function(){
var month = month_select.options[mySelect.selectedIndex].value, days;
if(month == 2){ // Feb
days = 28; // or 29 if you want to check for the leap year as well
}else if(month % 2){ // if the month is "even"
days = month <= 7 : 30 : 31; // if the month is odd and between Jan - July, then it will have 30 days, otherwise it will have 31
}else{
days = month <= 7 : 31 : 30; // similar to above but when months are odd
}
// update days via DOM here
var options = month_select.options, options_length = options.length;
if(days > options_length){ // if it has options in days then the currently selected month, then remove some
}else if(days < options_length){ // if it has less, then add
} // if it has the same number, then don't do anything
}
【讨论】: