简体   繁体   English

根据在另一个选择下拉列表中选择的年份动态显示选择下拉列表中剩余的可用月份

[英]Dynamically display available months left in a select dropdown based on Year selected in another select dropdown

I have a table that has the months from January to December with values 1 to 12. On my page i have a dropdown with years from current year.我有一张表,其中包含从 1 月到 12 月的月份,值为 1 到 12。在我的页面上,我有一个从当年开始的年份的下拉列表。 I also have another dropdown that displays the months.我还有另一个显示月份的下拉菜单。

By setting the current month, I can get the dropdown of months to display current month and the remaining month of the year.通过设置当前月份,我可以得到月份的下拉列表来显示当前月份和一年中剩余的月份。 What I need help for is for the month dropdown to display available months remaining based on year selected from the year dropdwon.我需要帮助的是月份下拉菜单根据从 dropdwon 中选择的年份显示剩余的可用月份。 That is if I select 2019, I should see only December(current month and last month of year).也就是说,如果我选择 2019,我应该只看到 12 月(当月和去年的最后一个月)。 If I pick 2020, I should see January to December.如果我选择 2020 年,我应该会看到 1 月到 12 月。

I have checked other solutions, but it does not help with what I need.我已经检查了其他解决方案,但它对我所需要的没有帮助。 Some of the solutions seen have the month names as an array but the corresponding values are not included.看到的一些解决方案将月份名称作为数组,但不包括相应的值。

My php code below:我的php代码如下:

$curmonth = date('n');

$query = "SELECT calmonthsID, months, value from calmonths WHERE value>=? ORDER BY calmonthsID ASC";
$stmt = $connQlife->prepare($query);
$stmt->bind_param('s', $curmonth);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($calmonthsID, $months, $value);

The html code below: html代码如下:

<select name="year" id="year" value="" tabindex="1" aria-hidden="true" required style="height:50px;display: block;padding:10px;" class="h5">
   <option value="" selected="selected">- Select Year -</option>
   <option value="2019">2019</option>
   <option value="2020">2020</option>
   <option value="2021">2021</option>
   <option value="2022">2022</option>
</select>

<select name="month" id="month" value="" tabindex="1" aria-hidden="true" required style="height:50px;display: block;padding:10px;" class="h5">
    <option value="" selected="selected">- Select One -</option>
    <?php while($stmt->fetch()) { ?>
        <option value=<?Php echo $value; ?>><?Php echo $months; ?></option>                             
    <?php } $stmt->close(); ?>
</select>

If you don't want to use AJAX, I may suggest following workaround:如果您不想使用 AJAX,我可能会建议以下解决方法:

  1. Render all possible select-month inputs outside of the form with ids like `month-${year}` and set style.display:none .在表单之外呈现所有可能的选择月份输入,ID 为 `month-${year}` 并设置style.display:none
  2. In select-year input make onChange - copy (or move) needed select-month into form & unhide it.在 select-year 输入中 make onChange - 将需要的 select-month 复制(或移动)到表单中并取消隐藏它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据nodejs中的另一个下拉列表动态选择下拉列表 - Dynamically select dropdown based on another dropdown in nodejs 根据在选择/下拉框中而不是复选框中选择的值动态获取和显示地图图层 - Dynamically fetch and display map layers based on the value selected in a select/dropdown box instead of checkbox 根据另一个下拉列表选择一个下拉列表的值 - select value of one dropdown based on another dropdown 如果选择了另一个选择下拉列表中的一个选项,如何显示选择下拉列表 - How do I display a select dropdown list when one option on another select dropdown is selected 如何在jsp中显示选定年份(年份)和月份(月份)中的所有日历日期? - How can I display all the calendar dates in jsp, for a selected year in years dropdown and selected month in months dropdown? 如何在没有任何提交按钮的情况下动态显示基于下拉菜单中所选年份的记录 - How to display records based on selected year from dropdown dynamically without any submit button 如何根据其他下拉选择的值显示下拉选择? - How to display a dropdown select based on the value of other dropdown select? 如何在选择下拉列表中显示所选复选框的值? - How to display selected checkboxes' values in select dropdown? 根据另一个选择下拉列表过滤一个HTML选择下拉列表 - Filter one HTML select dropdown based on another select dropdown 根据所选项目填写选择下拉列表 - Fill select dropdown based on selected item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM