要动态地迭代几个月的集合,您希望将月份存储在
Map<Integer, String>其中键是月份数字而值是月份名称的位置。要使HTML
<option>元素默认处于选中状态,您需要设置
selected属性。
因此,假设您在范围内有a
Map<Integer, String> months和a
Integer selectedMonth,则应执行以下操作:
<select name="birthday_month"> <c:forEach items="${months}" var="month"> <option value="${month.key}" ${month.key == selectedMonth ? 'selected' : ''}>${month.value}</option> </c:forEach></select>当等于当前迭代的月份数时,条件运算符
?:将打印。
selected``selectedMonth



