是否可以自动执行单击'事件,而不是按下按钮?
是否可以自动执行“单击”事件,而不是按下按钮? 我找到了一些有关“变更”事件的信息,但不知道如何在我的代码中正确实现此信息。
这是我的代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Checkboxes</title>
</head>
<body>
<p>Kies je gewenste optie:</p>
<label for="c1"> <input type="checkbox" name="optie" value="vlaggen" id="c1">Vlaggen</label>
<label for="c2"><input type="checkbox" name="optie" value="frontbache" id="c2">Frontbache</label>
<label for="c3"><input type="checkbox" name="optie" value="achterbache" id="c3">Achterbache</label>
<p>
<button id="btn">Get Selected Colors</button>
</p>
<script>
const btn = document.querySelector('#btn');
btn.addEventListener('click', (event) => {
let checkboxes = document.querySelectorAll('input[name="optie"]:checked');
let values = [];
checkboxes.forEach((checkbox) => {
values.push(checkbox.value);
});
if (values == "vlaggen")
{
alert("vlaggen is geselecteerd");
}
else if (values == "frontbache")
{
alert("Frontbache is geselecteerd");
}
else (values == "achterbache")
{
alert("Achterbache is geselecteerd");
}
});
</script>
</body>
</html>
Is it possible to automatically execute the 'click' event, instead of pushing the button?
I found some info about the 'change' event but don't know how to implement this properly in my code.
This is my code:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Checkboxes</title>
</head>
<body>
<p>Kies je gewenste optie:</p>
<label for="c1"> <input type="checkbox" name="optie" value="vlaggen" id="c1">Vlaggen</label>
<label for="c2"><input type="checkbox" name="optie" value="frontbache" id="c2">Frontbache</label>
<label for="c3"><input type="checkbox" name="optie" value="achterbache" id="c3">Achterbache</label>
<p>
<button id="btn">Get Selected Colors</button>
</p>
<script>
const btn = document.querySelector('#btn');
btn.addEventListener('click', (event) => {
let checkboxes = document.querySelectorAll('input[name="optie"]:checked');
let values = [];
checkboxes.forEach((checkbox) => {
values.push(checkbox.value);
});
if (values == "vlaggen")
{
alert("vlaggen is geselecteerd");
}
else if (values == "frontbache")
{
alert("Frontbache is geselecteerd");
}
else (values == "achterbache")
{
alert("Achterbache is geselecteerd");
}
});
</script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我正确理解您,则需要在不单击按钮的情况下显示值(这是您问题的标题)。因此,当用户检查框时,应显示结果。
第一个解决方案:
您可以添加
input
onChange()
,当用户检查任何选项时,结果将弹出。第二个解决方案:
您也可以通过jQuery
添加头部
来实现此目标
If I understand you correctly, you need to display the values without clicking the button (this is from the title of your question). So when the user checked the box, the result should be shown.
First Solution:
you can add in the
input
anonChange()
, when the user checks any option, the result will then pop up.Second Solution:
you can also achieve this by jQuery
add in the head
使用
“更改”
事件无需单击按钮即可自动换取警报,我希望以下代码能为您提供帮助:Use
"change"
event automatically excute alert without click button, i hope below code will help you:您可以尝试此活动,
希望这可以为您提供帮助。
You can try this event
I hope this can help you.