Javascript 延迟检查循环中的单选按钮
我在同一组中有三个单选按钮。 第一个有一个Javascript 延迟检查循环中的单选按钮,javascript,jquery,html,Javascript,Jquery,Html,我在同一组中有三个单选按钮。 第一个有一个checked=“checked”,其他两个被禁用: (使用Jquery或javascript)是否可以: -以2秒的延迟自动选中组中的“下一步”按钮(取消选中上一个按钮),然后依次选中“下一步”按钮,直到它到达最后一个收音机, -然后循环回到开头(第一个收音机)并再次开始检查 谢谢你的帮助 编辑:更新到目前为止我得到的代码:首先,您需要在项目中包含JQuery。 第二,使用以下脚本: var index = 1; var change = funct
checked=“checked”
,其他两个被禁用:
(使用Jquery或javascript)是否可以:-以2秒的延迟自动选中组中的“下一步”按钮(取消选中上一个按钮),然后依次选中“下一步”按钮,直到它到达最后一个收音机,
-然后循环回到开头(第一个收音机)并再次开始检查 谢谢你的帮助
编辑:更新到目前为止我得到的代码:首先,您需要在项目中包含JQuery。 第二,使用以下脚本:
var index = 1;
var change = function () {
setTimeout(function () {
if (index > 3)
index = 0;
$("#test-"+index).click();
index++;
change();
},
2000);
}
$(function () {
change();
});
试试这个:
function checknext(){
checkedbutton=$('input[name=testing]:checked');
if($(checkedbutton).is(':last-child'))
$('div input').first().prop("checked", true );
else
$(checkedbutton).next().prop("checked", true );
}
setInterval(function(){
checknext();
},2000)
HTML:
<input id="test-1" type="radio" name="testing" checked />
<input id="test-2" type="radio" name="testing" />
<input id="test-3" type="radio" name="testing" />
<input id="test-1" type="radio" name="testing" checked />
<input id="test-2" type="radio" name="testing" />
<input id="test-3" type="radio" name="testing" />
<br /><br />
<a href="#" id="start">Start</a>
<a href="#" id="stop">Stop</a>
($('input:checked')。index()+1)%3
将返回0
、1
或2
,并将选中相应的单选按钮
setInterval()
每2000毫秒运行一次函数
带停止/启动按钮
HTML:
<input id="test-1" type="radio" name="testing" checked />
<input id="test-2" type="radio" name="testing" />
<input id="test-3" type="radio" name="testing" />
<input id="test-1" type="radio" name="testing" checked />
<input id="test-2" type="radio" name="testing" />
<input id="test-3" type="radio" name="testing" />
<br /><br />
<a href="#" id="start">Start</a>
<a href="#" id="stop">Stop</a>
在这里,每次触发函数
startCycle
时,我都会为循环分配一个间隔ID,然后使用该间隔ID停止循环。这里您可以看到如何设置延迟,setTimeout
:
setTimeout( expression, timeout )
e、 g
在这里,您可以看到如何检查:
把这两个和‘尤里卡!’结合起来 无限循环:
<div id="input-wrapper"><input type="radio" name="testing" checked="checked" />
<input disabled="disabled" type="radio" name="testing" />
<input disabled="disabled" type="radio" name="testing" /></div>
(function($) {
$(function() {
var inputs = $('#input-wrapper').find('input'),
delay = 1000,
index = 0,
change = setInterval(function() {
if (index >= inputs.length) index = 0;
inputs.removeAttr('checked').not(inputs.eq(index)).attr('disabled', 'disabled');
inputs.eq(index).removeAttr('disabled').attr('checked', 'checked');
index++;
}, delay);
});
})(jQuery);
(函数($){
$(函数(){
var inputs=$(“#输入包装器”).find('input'),
延迟=1000,
指数=0,
change=setInterval(函数(){
如果(索引>=inputs.length)索引=0;
inputs.removeAttr('checked').not(inputs.eq(index)).attr('disabled','disabled');
inputs.eq(index).removeAttr('disabled').attr('checked','checked');
索引++;
},延误);
});
})(jQuery);
请注意,较新版本的jquery使用prop方法而不是attr方法
基本上,计时器每秒触发checkRadio()函数,该函数根据变量i将单选按钮设置为激活状态 var i=1
function checkRadio(){
$("#test-" + i).prop("checked", true)
if(i == 3){
i = 0;
}else{
i++;
}
}
var timer = window.setInterval(function(){
checkRadio();
},1000);
纯Javascript(简短)版本:
功能检查_next(){
var=document.getElementsByName(“测试”);
对于(变量i=0;i<1.length;++i){
if(需要[i]。选中==true){
if(i==wanted.length-1)
{
需要[0]。选中=true;
}否则{
需要[i+1]。选中=真;
}
打破
}
}
}
setInterval(函数(){
检查下一步()
}, 1000)
是的,这是可能的……第一:您使用同一id三次,第二:其他输入未被禁用,第三:您可以使用间隔函数来执行。到目前为止你有什么密码?是的,我已经修好了身份证。还更新了小提琴与所有我到目前为止。我不擅长javascript,所以我不知道如何从这里开始。id应该是不同的,这意味着我们可以做很多事情-有用:)这是IMHO最优雅的解决方案。应该是.prop('checked',true)
但是。是的,将其更改为true。有没有办法启动/停止循环?@dd5我添加了启动/停止按钮。也感谢您的输入!关于OP发生了什么的描述?第一次发布不知道,基本上计时器每秒都会触发checkRadio()函数,该函数根据变量iMight将单选按钮设置为活动状态,希望将其添加到您的答案中:)欢迎使用StackOverflow!
function checkRadio(){
$("#test-" + i).prop("checked", true)
if(i == 3){
i = 0;
}else{
i++;
}
}
var timer = window.setInterval(function(){
checkRadio();
},1000);
function Check_next() {
var wanted = document.getElementsByName("testing");
for (var i = 0; i < wanted.length; ++i) {
if (wanted[i].checked == true) {
if (i == wanted.length - 1)
{
wanted[0].checked = true;
} else {
wanted[i + 1].checked = true;
}
break;
}
}
}
setInterval(function () {
Check_next()
}, 1000)