我在 javascript 中为我的表单生成了一个下拉列表。但是,当我发布表单时,下拉列表的值为空。即使我从列表中选择另一个选项,这意味着它不能是默认的。这是我的PHP:
$BirthDays = ($_POST['Days']);
$BirthMonths = ($_POST['Months']);
$BirthYears = ($_POST['Years']);
这是javascript:
function runcalender() {
var calendar = [
["January", 31],
["February", 28],
["March", 31],
["April", 30],
["May", 31],
["June", 30],
["July", 31],
["August", 31],
["September", 30],
["October", 31],
["November", 30],
["December", 31]
],
cont = document.getElementById('calender');
//set variables for calender
var sel_year = document.createElement('select'),
sel_month = document.createElement('select'),
sel_day = document.createElement('select');
sel_year.setAttribute("id", "Years");
sel_month.setAttribute("id", "Months");
sel_day.setAttribute("id", "Days");
sel_year.setAttribute("name", "Years");
sel_month.setAttribute("name", "Months");
sel_day.setAttribute("name", "Days");
function createOption(txt, val) {
var option = document.createElement('option');
option.value = val;
option.appendChild(document.createTextNode(txt));
return option;
}
function clearChildren(ele) {
while (ele.hasChildNodes()) {
ele.removeChild(ele.lastChild);
}
}
function recalculateDays() {
var month_index = sel_month.value,
df = document.createDocumentFragment();
for (var i = 0, l = calendar[month_index][1]; i < l; i++) {
df.appendChild(createOption(i + 1, i));
}
clearChildren(sel_day);
sel_day.appendChild(df);
}
function generateMonths() {
var df = document.createDocumentFragment();
calendar.forEach(function(info, i) {
df.appendChild(createOption(info[0], i));
});
clearChildren(sel_month);
sel_month.appendChild(df);
}
function generateYears(){
var dateForYear = new Date();
var x = dateForYear.getFullYear();
var y = x - 100;
var df = document.createDocumentFragment();
for (i = x; i >= y; i--){
df.appendChild(createOption(i, i));
}
clearChildren(sel_year);
sel_year.appendChild(df);
}
sel_month.onchange = recalculateDays;
generateMonths();
recalculateDays();
generateYears();
cont.appendChild(sel_day);
cont.appendChild(sel_month);
cont.appendChild(sel_year);
}
我是否需要让 javascript 手动选择结果以便 PHP 识别它?