在一段时间后随机检查单选按钮

时间:2015-10-25 22:25:36

标签: javascript jquery

如何在一段时间后随机检查新的单选按钮?

我喜欢归档已检查的单选按钮每5秒自动更改一次的效果。谢谢!

<input type="radio" id="mc" name="radiobutton" value="Mastercard" checked>
<label for="mc"> Mastercard</label><br> 
<input type="radio" id="vi" name="radiobutton" value="Visa">
<label for="vi">  Visa</label><br> 
<input type="radio" id="ae" name="radiobutton" value="AmericanExpress">
<label for="ae"> American Express</label> 

1 个答案:

答案 0 :(得分:2)

您可以使用setInterval

&#13;
&#13;
var $inputs = $("input[type=radio]");
setInterval(function() {
  var random = Math.floor(Math.random() * $inputs.length);
  $inputs.each(function(i, node) {
    node.checked = (i === random);
  });
}, 5000); // 5000 = 5 seconds
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="radio" id="mc" name="radiobutton" value="Mastercard" checked>
<label for="mc">Mastercard</label>
<br>
<input type="radio" id="vi" name="radiobutton" value="Visa">
<label for="vi">Visa</label>
<br>
<input type="radio" id="ae" name="radiobutton" value="AmericanExpress">
<label for="ae">American Express</label>
&#13;
&#13;
&#13;