1

我在 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 识别它?

4

1 回答 1

2

我假设您的意思是 PHP 正在接收 POSTed 变量,但下拉列表未显示所选结果。

如果是这种情况(并且因为您在页面加载时通过 JS 呈现 HTML),您需要 PHP 向 JavaScript 提供信息,以便它可以显示它是选定的结果。我提出以下建议。编辑createOption函数...

function createOption(txt, val, isSelected) {
    var option = document.createElement('option');
    option.value = val;
    if(isSelected) option.selected = 'selected'
    option.appendChild(document.createTextNode(txt));
    return option;
}

然后将一些 Javascript 写入读取 POST 变量值的页面。您的页面需要 *.php 才能正常工作(取决于服务器配置)

<script language='text/javascript'>
   //Don't forget to sanitize your data, I won't cover that here...
   var postDays = <?php print $_POST['Days']; ?>;
   var postMonths = <?php print $_POST['Months']; ?>;
   var postYears = <?php print $_POST['Years']; ?>;
</script>

最后,当您运行该createOption函数时,给出一个布尔isSelected值(true 或 false),将值(createOption()'val' 参数)与新的相关 JS 变量进行比较......(postDays例如)。如果val匹配postDays,则为 true,如果不匹配,则为 false。

另外,我强烈推荐学习 jQuery。您可能会在四分之一的时间内编写相同的代码块。它有很多功能可以使您尝试做的事情自动化。

于 2013-10-17T04:16:27.510 回答