2017-08-11 72 views
4

请问为什么下面的代码不能通过1,2,3,1,2,3顺序(jsfiddle
而我继续按“下一步” 。jquery上一个和下一个幻灯片顺序重复

当我在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; 
}); 
+1

该示例适用于我在Chrome和Firefox上的工作。 –

+0

...我不能在Firefox和铬。为什么 –

+0

是否检查错误日志? –

回答

1

在这里你去多一个办法http://jsfiddle.net/hTgv3/162/

$('#next').click(function() { 
 
    if($('.getradiotomove:checked').parent().is(':last-child')){ 
 
    \t $('ul[class="options-list"] > li') 
 
     \t .first() 
 
     .find('.getradiotomove') 
 
\t  .prop('checked', true); 
 
    } else { 
 
    \t $('.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>

希望这会帮助你解决问题。

+0

谢谢你的回答。 –

+0

欢迎@taikachan :) – Shiladitya

+0

它在纯html文件中工作。然而,如果我把我的网站(有很多插件),它不能使它连续。但是,您的解决方案对我来说是最好的。 –