javascript - 使用 JavaScript 从选择标签获取数据

标签 javascript html

我有以下 JavaScript 方法

// METHOD 1
let categoryType = document.getElementById("create-listing-category-type");
let categoryTypeOption = categoryType.getElementsByTagName("option")[categoryType.selectedIndex].getAttribute("value");


// METHOD 2
let categoryType = document.getElementById("create-listing-category-type");
let categoryTypeOption = categoryType.options[categoryType.selectedIndex].value;
<div class="verification-main-input-div">
  <p class="verification-main-text">Category</p>

  <div class="drop-dawn-add-game-list">
    <div class="arrow-drop-down-games-main">
      <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-chevron-right chevron-for-drop-down"><polyline points="9 18 15 12 9 6"></polyline></svg>
    </div>
    <select id="create-listing-category-type" name="cars" class="drop-dawn-add-game-select">
      <option value="Coins">Coins</option>
      <option value="Items">Items</option>
      <option value="Boosting">Boosting</option>
    </select>
  </div>
</div>

我想要完成的是从 <select> 获取所选选项的数据标签。当我只是 console.log(categoryTypeOption);我得到默认值,即 Coins ,无论我是否选择另一个。我做错了什么?

这两种方法基于我在研究解决方案时找到的答案。

Get selected value in dropdown list using JavaScript

最佳答案

你的方法2有效...问题是你没有触发你的categoryTypeOption变量的重新计算。引用下面的代码片段...我向 select 标记添加了一个 onchange 事件处理程序。并将方法 2 代码移至处理函数中...

function onSelectChange() {
  let categoryType = document.getElementById("create-listing-category-type");
  let categoryTypeOption = categoryType.options[categoryType.selectedIndex].value;
  console.log(categoryTypeOption);
}
<div class="verification-main-input-div">
  <p class="verification-main-text">Category</p>
  <div class="drop-dawn-add-game-list">
    <div class="arrow-drop-down-games-main">
      <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-chevron-right chevron-for-drop-down">
        <polyline points="9 18 15 12 9 6"></polyline></svg>
    </div>
    <select id="create-listing-category-type" name="cars" class="drop-dawn-add-game-select" onchange="onSelectChange()">
      <option value="Coins">Coins</option>
      <option value="Items">Items</option>
      <option value="Boosting">Boosting</option>
    </select>
  </div>
</div>

关于javascript - 使用 JavaScript 从选择标签获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62798126/

相关文章:

javascript - 如何在 Angular 中导入模块?

javascript - 为什么我的 div 没有隐藏,在 jQuery 中?

javascript - 从对象属性在 Canvas 上绘制图像

javascript - 替换JS语言文件加载旧值

javascript - javascript继承中正确的原型(prototype)伪装是什么?

javascript - 如何获取跨度的行高?

css - 带有箭头的 HTML div 评论框

html - jumbotrons 和导航栏周围的 Bootstrap 填充?

html - Bootstrap 没有官方侧边栏菜单吗?

javascript - 如何将http重定向到https加上将 `index.html`重定向到.htaccess中的另一个html页面?