通过更改侦听器获取选项

发布于 2025-02-13 06:29:13 字数 919 浏览 0 评论 0原文

如何通过Typescript通过更改侦听器获得所选选项? 我找到了JavaScript示例,其中通过>获得该值,但找不到任何包含所选选项的字段。

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="script.js"></script>
  </head>
  <body>
    <div>
      <label>Select:</label>
      <select id="select1">
        <option value="a">A</option>
        <option value="b">B</option>
        <option value="c">C</option>
      </select>
    </div>
  </body>
</html>
window.addEventListener("load", handleLoad);

function handleLoad(event: Event): void {
  let select: HTMLDivElement = <HTMLDivElement>(
    document.querySelector("#select1")
  );
  select.addEventListener("change", getSelectedOption);
}

function getSelectedOption(event: Event): void {
  console.log("Selected option:", event);
}

How to get the selected option via change listener with TypeScript?
I have found JavaScript examples where the value is obtained via event., but I could not find any field that contained the selected option.

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="script.js"></script>
  </head>
  <body>
    <div>
      <label>Select:</label>
      <select id="select1">
        <option value="a">A</option>
        <option value="b">B</option>
        <option value="c">C</option>
      </select>
    </div>
  </body>
</html>
window.addEventListener("load", handleLoad);

function handleLoad(event: Event): void {
  let select: HTMLDivElement = <HTMLDivElement>(
    document.querySelector("#select1")
  );
  select.addEventListener("change", getSelectedOption);
}

function getSelectedOption(event: Event): void {
  console.log("Selected option:", event);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美胚控场 2025-02-20 06:29:13

谢谢Bloodyknuckles,我通过结果错误消息找到了解决方案:

function getSelectedOption(event: Event): void {
  console.log("Selected option:", (<HTMLInputElement>event.target).value);

Thanks bloodyKnuckles, I found the solution via the resulting error message:

function getSelectedOption(event: Event): void {
  console.log("Selected option:", (<HTMLInputElement>event.target).value);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文