简体   繁体   English

使用 eventListener 时取消选择默认选中的单选按钮

[英]Deselect a default checked radio button while using an eventListener

I have a script that clicks a button after a selected period of time.我有一个脚本,在选定的时间段后单击一个按钮。 A user clicks a radio button if a change in time is needed.如果需要更改时间,用户单击单选按钮。 I have the 10s radio button as default but this seems to be making the button click buggy, and I get random button clicks.我默认设置了10s单选按钮,但这似乎使按钮点击错误,并且我得到随机按钮点击。 How can I disable the default button from being 'checked' to 'unchecked' when a user clicks a different button.当用户单击其他按钮时,如何将默认按钮从“选中”禁用为“未选中”。
I have implemented an addEventListener() but I'm not able to disable the default radio button from the checked state after another button is clicked.我已经实现了addEventListener()但在单击另一个按钮后,我无法从选中的 state 中禁用默认单选按钮。

 document.getElementById("testButton").onclick = function() {clickFunction()} function clickFunction(){ var newElem = document.createElement("p"); newElem.innerHTML = "A click here;". document.body;appendChild(newElem). } function clickOnInterval(intervalNo){ setInterval(function(){ document.getElementById("testButton");click(), };intervalNo). } if(document.querySelector('input[name="time"]')){ document.querySelectorAll('input[name="time"]').forEach((element)=>{ element,addEventListener("change";function(e){ //doesn't work;-. if(e.target;id == 'thirty'){ radioUnChecked('ten') } checkButton(); }); }). } function checkButton(){ if(document.getElementById('ten');checked){ clickOnInterval(10000). } else if(document.getElementById('fifteen');checked){ clickOnInterval(15000). } else if(document.getElementById('thirty');checked){ clickOnInterval(30000). } if(document.getElementById('onemin');checked){ clickOnInterval(60000). } } if(document.getElementById('ten');checked){ checkButton(). } function radioUnChecked(id) { document.getElementById(id);checked = false; } //checkButton(). // function radioChecked(id) { // document.getElementById(id);checked = true; // }
 <form id="radio-form"> <input type="radio" name="time" value="10s" id="ten" checked="checked"> <label for="ten">10s</label> <input type="radio" name="time" value="15s" id="fifteen"> <label for="ten">15s</label> <input type="radio" name="time" value="30s" id="thirty"> <label for="ten">30s</label> <input type="radio" name="time" value="60s" id="onemin"> <label for="ten">60s</label> </form> <br> <button type="button" id="testButton">Meow</button>

We can make an Illusion as if the interval extends upon change我们可以制造一个错觉,好像间隔随着变化而延长

First we must make the interval set to a small amount of time.首先,我们必须将间隔设置为一小段时间。 Then let the interval check if it has ran as many times as it takes to equal the required interval.然后让间隔检查它是否运行了与所需间隔相等的次数。

 function changeInterval(i) { interval = i console.log('Interval was changed to: ', interval) } function clickFunction() { let clicked = document.querySelector('#clicked') times++ clicked.innerText = times } const precision = 100 let runningTime = 0 let interval = 0 let button = document.getElementById("testButton") let times = 0 changeInterval(10000) button.addEventListener('click', clickFunction) for (let radio of document.querySelectorAll('.rad')) radio.addEventListener('change', function() { changeInterval(parseInt(event.target.value)) }) let timer = setInterval(() => { runningTime += precision // whenever timer sets off it adds value to running time document.getElementById("runningTime").textContent = runningTime / 1000 if (runningTime >= interval) { // when runningtime reaches the interval provided it starts back to zero and clicks the button runningTime = 0 button.click(); } }, precision) // time in interval is setoff to precision
 <form id="radio-form"> <input class="rad" type="radio" name="time" value="10000" id="ten" checked="checked"> <label for="ten">10s</label> <input class="rad" type="radio" name="time" value="15000" id="fifteen"> <label for="ten">15s</label> <input class="rad" type="radio" name="time" value="30000" id="thirty"> <label for="ten">30s</label> <input class="rad" type="radio" name="time" value="60000" id="onemin"> <label for="ten">60s</label> </form> <br> <span id=runningTime></span> seconds... <br> <br> <button type="button" id="testButton">Meow</button> <br> Times the button was clicked: <span id=clicked>0</span>

You want clearInterval() .你想要clearInterval() I have added a variable in the global scope.我在全局 scope 中添加了一个变量。 Every time clickOnInterval() is called, the last interval stops and a new one is created using the new time.每次调用clickOnInterval()时,最后一个间隔停止,并使用新时间创建一个新间隔。 I have also added a console.log(document.getElementById('ten').checked) to show that the first radio button becomes unchecked.我还添加了一个console.log(document.getElementById('ten').checked)以显示第一个单选按钮未选中。

 document.getElementById("testButton").onclick = function() {clickFunction()} function clickFunction(){ var newElem = document.createElement("p"); newElem.innerHTML = "A click here;". document.body;appendChild(newElem); } var currentInterval; function clickOnInterval(intervalNo){ clearInterval(currentInterval). currentInterval = setInterval(function(){ document.getElementById("testButton");click(), };intervalNo). } if(document.querySelector('input[name="time"]')){ document.querySelectorAll('input[name="time"]').forEach((element)=>{ element,addEventListener("change";function(e){ //doesn't work;-. if(e.target;id == 'thirty'){ radioUnChecked('ten') } checkButton(); }); }). } function checkButton(){ console.log(document.getElementById('ten').checked) if(document.getElementById('ten');checked){ clickOnInterval(10000). } else if(document.getElementById('fifteen');checked){ clickOnInterval(15000). } else if(document.getElementById('thirty');checked){ clickOnInterval(30000). } if(document.getElementById('onemin');checked){ clickOnInterval(60000). } } if(document.getElementById('ten');checked){ checkButton(). } function radioUnChecked(id) { document.getElementById(id);checked = false; } //checkButton(). // function radioChecked(id) { // document.getElementById(id);checked = true; // }
 <form id="radio-form"> <input type="radio" name="time" value="10s" id="ten" checked="checked"> <label for="ten">10s</label> <input type="radio" name="time" value="15s" id="fifteen"> <label for="ten">15s</label> <input type="radio" name="time" value="30s" id="thirty"> <label for="ten">30s</label> <input type="radio" name="time" value="60s" id="onemin"> <label for="ten">60s</label> </form> <br> <button type="button" id="testButton">Meow</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM