document.addEventListener("DOMContentLoaded", () => { /** * 診断結果振り分け */ const showResultButton = document.querySelector(".btn-show-result"); if (showResultButton) { showResultButton.addEventListener("click", function (e) { e.preventDefault(); const selectedValues = {}; const radioButtons = document.querySelectorAll('.qa_item input[type="radio"]:checked'); //診断結果ページ遷移先リスト const destinationList = [ 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 3, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 9, 9, 8, 8, 9, 9, 8, 8, 9, 9, 8, 8, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, ]; // ラジオボタンの値を取得 radioButtons.forEach((radio) => { selectedValues[radio.name] = radio.value; }); // 全部選択したかチェック const totalQuestions = document.querySelectorAll(".qa_item").length; if (Object.keys(selectedValues).length < totalQuestions) { alert("全ての質問に回答してください。"); return; } // 回答を2進数の文字列にする let binaryString = ""; for (let i = 1; i <= Object.keys(selectedValues).length; i++) { binaryString += selectedValues[`q${i}`]; } // 2進数を10進数に変換 let answers = parseInt(binaryString, 2); // 結果ページへ遷移 const result = (window.location.href = `result/${String(destinationList[answers]).padStart( 2, "0" )}/index.aspx`); return false; }); } });