// select が変更されたら hidden の categoryId を更新する
$(document).on("change", ".categorySelect", function () {
const row = $(this).closest("tr");
const selected = $(this).val();
row.find(".categoryId").val(selected);
});
// 登録ボタン押下 → テーブル全行をまとめて送信
$("#registerBtn").on("click", function () {
const dataList = [];
$("#itemTable tbody tr").each(function () {
dataList.push({
itemId: $(this).find(".itemId").val(),
categoryId: $(this).find(".categoryId").val()
});
});
// まとめて Ajax 送信
$.ajax({
url: "/items/updateCategory",
type: "POST",
contentType: "application/json",
data: JSON.stringify(dataList),
success: function () {
alert("登録が完了しました");
},
error: function () {
alert("エラーが発生しました");
}
});
});