【问题标题】:jquery prev and next in slideshow in order repeatedlyjquery prev和next在幻灯片中重复顺序
【发布时间】:2018-01-19 09:48:55
【问题描述】:

想问一下为什么下面的代码不能按顺序走1,2,3,1,2,3(jsfiddle)
而我继续按“下一步”。

当我在 Chrome 中打开并运行以下代码时,它无法按“1,2,3”的顺序运行代码(它在 3 处停止)。

HTML

<div id="slides">
   <ul class="options-list">
   <li><input type="radio" class="getradiotomove" id="bundle-73" name="bundle_option"checked="checked" value="73"></li>
   <li><input type="radio" class="getradiotomove" id="bundle-72" name="bundle_option" value="72"></li>
   <li><input type="radio" class="getradiotomove" id="bundle-74" name="bundle_option" value="74"></li>
</div>

<div id="buttons">
   <a href="#" id="prev">prev</a>
   <a href="#" id="next">next</a>
   <div class="clear"></div>
</div>

JS

$('#next').click(function() {
    var $all     = $('.getradiotomove');
    var $current = $('.getradiotomove:checked');

    var index = $all.index($current) + 1;
    if(index >= $all.length)
        index = 0;
    $($all[index]).attr('checked', 'checked');
    return false;
});

【问题讨论】:

  • 该示例适用于我在 chrome 和 firefox 上。
  • ...我无法在 Firefox 和 chrome 中制作它。为什么
  • 你检查错误日志了吗?
  • 没有错误.. .....
  • 检查调试器并逐个执行代码

标签: jquery slideshow next


【解决方案1】:

这里还有一种方法http://jsfiddle.net/hTgv3/162/

$('#next').click(function() {
    if($('.getradiotomove:checked').parent().is(':last-child')){
    	$('ul[class="options-list"] > li')
      	.first()
        .find('.getradiotomove')
	      .prop('checked', true);
    } else {
    	$('.getradiotomove:checked')
        .parent()
        .next('li')
        .find('.getradiotomove')
        .prop('checked', true);
    } 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="slides">
    <ul class="options-list">
        <li><input type="radio" class="getradiotomove" id="bundle-73" name="bundle_option" value="73" checked></li>
        <li><input type="radio" class="getradiotomove" id="bundle-72" name="bundle_option" value="72"></li>
        <li><input type="radio" class="getradiotomove" id="bundle-74" name="bundle_option" value="74"></li>
    </ul>
</div>

<div id="buttons">
    <a href="#" id="prev">prev</a>
    <a href="#" id="next">next</a>
    <div class="clear"></div>
</div>

希望这能帮助您解决问题。

【讨论】:

  • 欢迎@taikachan :)
  • 它适用于纯 html 文件。但是,如果我放入我的网站(带有很多插件),它就无法连续完成。但是,您的解决方案对我来说是最好的。
  • 让我们尝试调试您的问题。你能检查一下你在浏览器控制台中遇到了什么错误吗?
  • 除了这个特定的输入复选框之外,您是否在其他任何地方使用过 ".getradiotomove" 类??
  • 我已经更新了答案,检查一下。更新代码$('ul[class="options-list"] &gt; li')
猜你喜欢
  • 2012-09-29
  • 1970-01-01
  • 1970-01-01
  • 2015-07-28
  • 1970-01-01
  • 1970-01-01
  • 2015-04-21
  • 2012-02-02
  • 1970-01-01
相关资源
最近更新 更多