通过更改侦听器获取选项
如何通过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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
谢谢Bloodyknuckles,我通过结果错误消息找到了解决方案:
Thanks bloodyKnuckles, I found the solution via the resulting error message: