0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

メモ javascript

0
Posted at
// 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("エラーが発生しました");
        }
    });

});
function submitAll() {
    const rows = document.querySelectorAll("#itemTable tbody tr");
    const sendData = [];

    rows.forEach(row => {
        sendData.push({
            itemId: row.dataset.itemId,
            categoryId: row.querySelector("select").value
        });
    });

    // Ajax送信 or hidden生成
}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?