创建复选框以选择选择框中的所有选项

问题描述 投票:-1回答:1

我的第二个PHP任务要求我创建一个年份范围的选择框,一个带有12个月的第二个选择框,然后是一个复选框,用户可以选择是否要选择整年。我创建了复选框,但我不明白如何让它选择所有月份。我花了两个多小时试图找到一个解决方案而且我为没有为我的任务的这个特定部分进行代码尝试而道歉。老实说,我不知道从哪里开始。我对PHP的理解现在非常狭窄。我甚至不知道如何正确地为谷歌搜索这个问题。每次搜索都会向我提供有关如何创建允许多项选择的选择框的说明。我在创建“全选”选项时找到的唯一网站显示它是用JS完成的。我必须在PHP中这样做。

我应该补充一点,我只允许使用PHP和HTML以及一些带有这个赋值的CSS样式。

<!doctype html>
<html lang="en">
<head>
    <title>Calendar</title>
    <!--    <link rel="stylesheet" type="text/css" href="quote.css">-->
</head>
<body>
<section>
    <form id="yearForm" name="yearForm" method="post" action="">
        <label for="select_year">Select the year: </label>
        <?php
        // Sets the default year to be the current year.
        $date = new DateTime();
//        $current_year = date('Y');
        $current_year = $date->format('Y');
        // Year to start available options.
        $earliest_year = ($current_year - 5);
        // Set your latest year you want in the range.
        $latest_year = 2050;

        echo '<select>';
        // Loops over each int[year] from current year, back to the $earliest_year [1950]
        foreach ( range( $earliest_year, $latest_year ) as $i ) {
            // Echos the option with the next year in range.
            echo '<option value="'.$i.'" '.($i == $current_year ? ' selected="selected"' : '').'>'.$i.'</option>';
        }
        echo '</select>';
        ?>
    </form>
        <br />
        <br />
    <form id="monthForm" name="monthForm" method="post" action="">
        <label for="month">Select the month: </label>
<!--        <input type=hidden id="month" name=month>-->
                        <select id="month" name="month" >
                            <option value='01'>January</option>
                            <option value='02'>February</option>
                            <option value='03'>March</option>
                            <option value='04'>April</option>
                            <option value='05'>May</option>
                            <option value='06'>June</option>
                            <option value='07'>July</option>
                            <option value='08'>August</option>
                            <option value='09'>September</option>
                            <option value='10'>October</option>
                            <option value='11'>November</option>
                            <option value='12'>December</option>
                        </select>
                        <br />
                        <br />
        <label for="whole_year">Show whole year: </label>
        <input type="checkbox" id="whole_year" name="whole_year" >
        <br />
        <br />
        <input type="submit" class=inline name="submitButton" id="submitButton" value="Submit" />
    </form>
</section>
</body>
</html>
php
1个回答
1
投票

您可以使用select元素的multiple属性来选择多个选项:

<select name="modules[]" multiple="multiple">
   <option value="1">a</option>
   <option value="2">b</option>
   <option value="3">c</option>
   <option value="1,2,3">All</option>
</select>

现在,您可以选择多个选项。

© www.soinside.com 2019 - 2024. All rights reserved.