1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【HTML】セレクトボックスで動的に初期値を設定する

Posted at
1 / 2

概要

HTML(jsp)のセレクトボックスで動的に初期値を設定する。
具体的には以下の内容。
①Controller部分で初期値をスコープに格納
②Controller部分セレクトボックスの選択肢を作成&スコープに格納
③HTML(jsp)で初期値を設定した状態でセレクトボックス出力

ソースコード

Controller.java
// 初期値作成&格納
String currentYear = "2022年"
// ②セレクトボックスの選択肢作成&格納
List<String> yearList = new ArrayList<>();
yearList.add("2016年");
yearList.add("2017年");
yearList.add("2018年");
yearList.add("2019年");
yearList.add("2020年");
yearList.add("2021年");
yearList.add("2022年");
yearList.add("2023年");
yearList.add("2024年");
model.addAttribute("yearList ", yearList )

view.jsp
<form action="/arrangement/holiday" method="POST">
<select onchange="this.form.submit()" name="currentYear">
<c:forEach var="year" items="${yearList}" varStatus="status">
<c:choose>
<c:when test="${year == currentYear}">
<option selected>${year}</option>
</c:when>
<c:otherwise>
<option>${year}</option></c:otherwise>
</c:choose>
</c:forEach>
</select>
</form>	

完成イメージ

image.png
(*上記デザインいじっています。)

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?