array.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Example</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<select id="dropdown"></select>
<button id="button">送信</button>
<p id="result"></p>
<script src="script.js"></script>
</body>
</html>
array.js
$(document).ready(function() {
const data = {
"key1": "value1",
"key2": "value2",
"key3": "value3"
};
// Populate dropdown with the keys from the associative array
for (const key in data) {
$('#dropdown').append(`<option value="${key}">${key}</option>`);
}
// On button click, show the value corresponding to the selected key
$('#button').click(function() {
const selectedKey = $('#dropdown').val();
const selectedValue = data[selectedKey];
$('#result').text(`選択された値: ${selectedValue}`);
});
});