LoginSignup
0
0

More than 1 year has passed since last update.

HTMLから特定の値を返す

Posted at
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}`);
  });
});

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