我已经使用了上一个和下一个下拉列表。我想知道如何在每秒钟内自动更改下拉选项值。https://jsfiddle.net/wmwayysz/
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<button type="button" id="prev">Previous</button>
<select id="selectBox" onchange="currentSlide(value);" class="selectpicker">
<option value="1" class="options">Electronics</option>
<option value="2" class="options">Clothing</option>
<option value="3" class="options">Health</option>
<option value="4" class="options">Food</option>
<option value="5" class="options">Travel</option>
<option value="6" class="options">Mobiles</option>
<option value="7" class="options">Grocery</option>
<option value="8" class="options">Recharge</option>
<option value="9" class="options">Furniture</option>
下一个
$.fn.loopNext = function(selector) {
var selector = selector || '';
return this.next(selector).length ? this.next(selector) :
this.siblings(selector).addBack(selector).first();
}
$.fn.loopPrev = function(selector) {
var selector = selector || '';
return this.prev(selector).length ? this.prev(selector) :
this.siblings(selector).addBack(selector).last();
}
$("#next").click(function() {
$('#selectBox > option:selected')
.removeAttr('selected')
.loopNext('option')
.prop('selected', 'selected');
});
$("#prev").click(function() {
$('#selectBox > option:selected')
.removeAttr('selected')
.loopPrev('option')
.prop('selected', 'selected');
});
function currentSlide(selectionValue) {
console.log(selectionValue);
}
尝试此
setInterval(function() {
$('#next').trigger('click');
}, 30000)
您必须在接下来使用的时间间隔后调用相同的代码迭代。因此,通用代码进入函数。这是小提琴。https://jsfiddle.net/wmwayysz/2/
这是您的JavaScript更新代码
$.fn.loopNext = function(selector) {
var selector = selector || '';
return this.next(selector).length ? this.next(selector) : this.siblings(selector).addBack(selector).first();
}
$.fn.loopPrev = function(selector) {
var selector = selector || '';
return this.prev(selector).length ? this.prev(selector) : this.siblings(selector).addBack(selector).last();
}
$("#next").click(funShowNext);
setInterval(funShowNext,3000);
function funShowNext() {
$('#selectBox > option:selected')
.removeAttr('selected')
.loopNext('option')
.prop('selected', 'selected');
}
$("#prev").click(function() {
$('#selectBox > option:selected')
.removeAttr('selected')
.loopPrev('option')
.prop('selected', 'selected');
});
function currentSlide(selectionValue) {
console.log(selectionValue);
}