简体   繁体   English

单击单选类型按钮?

[英]Clicking Radio type buttons?

Well this java script should click the buttons (making an animation slideshow) the thing is i cant manage the code to click the button, and i cant find the error, seems to be running那么这个java脚本应该点击按钮(制作动画幻灯片),但我无法管理点击按钮的代码,我找不到错误,似乎正在运行

 <!DOCTYPE html> <html> <script type="text/javascript"> var seconds = 0; setInterval(setTime, 1000); function setTime() { seconds++; if (seconds == 5) { document.getElementById('i2').value = true; } else if (seconds == 10) { document.getElementById('i3').value = true; } else if (seconds == 15) { document.getElementById('i4').value = true; } else if(seconds == 20) { document.getElementById('i1').value = true; seconds == 0; } } </script> <div class="slideshow" onload="setTime()"> <input type="radio" id="i1" name="images" checked/> <input type="radio" id="i2" name="images" /> <input type="radio" id="i3" name="images" /> <input type="radio" id="i4" name="images" /> </div> </html>

I tried .checked .click() .value , its not working, something is messing up the action but again I cant figure out what it is.我试过.checked .click() .value ,它不工作,有些东西弄乱了动作,但我又不知道它是什么。

The value is the value that will be submitted with the form data if the radio button is checked.value是将与表单数据如果无线电按钮被选中要提交的值。

You need to modify the checked property if you want to change its state from unchecked to checked.如果要将其状态从未选中更改为选中,则需要修改已checked属性。

 var seconds = 0; setInterval(setTime, 1000); function setTime() { seconds++; if (seconds == 5) { document.getElementById('i2').checked = true; } else if (seconds == 10) { document.getElementById('i3').checked = true; } else if (seconds == 15) { document.getElementById('i4').checked = true; } else if (seconds == 20) { document.getElementById('i1').checked = true; seconds == 0; } }
 <input type="radio" id="i1" name="images" checked/> <input type="radio" id="i2" name="images" /> <input type="radio" id="i3" name="images" /> <input type="radio" id="i4" name="images" />

It is the checked property you have to set not the value .这是您必须设置的检查属性而不是value

Please Note: Your script is executing before the DOM is fully loaded.请注意:您的脚本在 DOM 完全加载之前执行。 Either you have to place the script at the end of the page or wrap the code with您要么必须将脚本放在页面的末尾,要么将代码用

DOMContentLoaded 已加载 DOM 内容

The DOMContentLoaded event is fired when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading. DOMContentLoaded 事件在初始 HTML 文档完全加载和解析后触发,无需等待样式表、图像和子框架完成加载。 A very different event load should be used only to detect a fully-loaded page.一个非常不同的事件加载应该只用于检测完全加载的页面。 It is an incredibly common mistake to use load where DOMContentLoaded would be much more appropriate, so be cautious.在 DOMContentLoaded 更合适的地方使用 load 是一个非常常见的错误,所以要小心。

You also have typo in seconds == 0;你也有错字seconds == 0; , should be the assignment operator ( = ) instead of comparison. , 应该是赋值运算符 ( = ) 而不是比较。

 <!DOCTYPE html> <html> <script type="text/javascript"> document.addEventListener("DOMContentLoaded", function(event) { var seconds = 0; setInterval(setTime, 1000); function setTime() { seconds++; if (seconds == 5) { document.getElementById('i2').checked = true; } else if (seconds == 10) { document.getElementById('i3').checked = true; } else if (seconds == 15) { document.getElementById('i4').checked = true; } else if(seconds == 20) { document.getElementById('i1').checked = true; seconds = 0; } } }); </script> <div class="slideshow" onload='setTime()'> <input type="radio" id="i1" name="images" checked/> <input type="radio" id="i2" name="images" /> <input type="radio" id="i3" name="images" /> <input type="radio" id="i4" name="images" /> </div> </html>

it had 2 typos cheked and == its working now它检查了 2 个拼写错误并且 == 现在可以正常工作了

 var seconds = 0; setInterval(setTime, 1000); function setTime() { seconds++; if (seconds == 5) { document.getElementById('i2').checked = true; } else if (seconds == 10) { document.getElementById('i3').checked = true; } else if (seconds == 15) { document.getElementById('i4').checked = true; } else if (seconds == 20) { document.getElementById('i1').checked = true; seconds = 0; } }
 <div class="slideshow" onload="setTime()"> <input type="radio" id="i1" name="images" checked/> <input type="radio" id="i2" name="images" /> <input type="radio" id="i3" name="images" /> <input type="radio" id="i4" name="images" /> <div>

I had == 0;我有 == 0; and cheked as an input.并检查作为输入。 Thanks for your suggestions谢谢你的建议

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

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