2017-05-23 27 views
0

我有我的滑动滑块,想知道如何通过next和prev按钮更改单选按钮,我不擅长js或jquery,如果您可以帮助我我会很乐意:(我如何通过下一个prev更改单选按钮

  <section class="center slider"> 
      <div class="type"> 
      <label><input type="radio" name="type" value="1" checked /><img src="./images/1.png"></label> 
      </div> 
      <div class="type"> 
      <label><input type="radio" name="type" value="2" /><img src="./images/2.png"></label> 
      </div> 
      <div class="type"> 
      <label><input type="radio" name="type" value="3" /><img src="./images/3.png"></label> 
      </div> 
      <div class="type"> 
      <label><input type="radio" name="type" value="4" /><img src="./images/4.png"></label> 
      </div> 
      <div class="type"> 
      <label><input type="radio" name="type" value="5" /><img src="./images/5.png"></label> 
      </div> 
      <div class="type"> 
      <label> <input type="radio" name="type" value="6"/><img src="./images/6.png"></label> 
      </div> 
      <div class="type"> 
      <label><input type="radio" name="type" value="7" /><img src="./images/7.png"></label> 
      </div> 
     </section> 

     <button type="button" data-role="none" class="slick-prev" aria-label="Previous" tabindex="0" role="button">Previous</button> 

     <button type="button" data-role="none" class="slick-next" aria-label="Next" tabindex="0" role="button">Next</button> 

我试图用一些JS更多:

<button type="button" onclick="dayNavigation('prev');"data-role="none" class="slick-next" aria-label="Next" tabindex="0" role="button">Next</button> 

<button type="button" onclick="dayNavigation('next');" data-role="none" class="slick-prev" aria-label="Previous" tabindex="0" role="button">Previous</button> 

dayNavigation = function (direction) { 
    var all = $('.slider input:radio'); 
    var current = $('.slider input:radio:checked'); 
    var index; 
    if (direction == 'slick-prev') { 
     index = all.index(current) - 1;  
    } else { 
     index = all.index(current) + 1;   
    } 

    if(index >= all.size()) index = 0; 
    all.eq(index).click(); 
    return false; 
}; 
+0

你尝试过这么远吗?你可能想看看[问] – user1859022

+0

我刚刚编辑我尝试 – razvai

+0

你可以请添加Css代码。并添加你在这个模型中使用的库 –

回答

0

您可以使用从JQuery的的eq()功能与.prop()功能相结合,实现这一

这里的工作小提琴:http://jsfiddle.net/jPfXS/111/

<button type="button" onclick="dayNavigation('prev');"data-role="none" class="slick-next" aria-label="Next" tabindex="0" role="button">Next</button> 

<button type="button" onclick="dayNavigation('next');" data-role="none" class="slick-prev" aria-label="Previous" tabindex="0" role="button">Previous</button> 

var index = 0; 
dayNavigation = function (direction) { 
    var curr = $('.slider input[name="type"]:checked'); 
    console.log(curr); 
    if(direction == 'next'){ 
     if(index > 0){ 
      $('input[name="type"]').eq(index-1).prop("checked", true); 
      curr.prop("checked", false); 
      index--; 
     } 
    } 
    else{ 
     if(index < $('input[name="type"]').length - 1){ 
      $('input[name="type"]').eq(index+1).prop("checked", true); 
      curr.prop("checked", false); 
      index++; 
     } 
    } 
}; 
+0

仍然不能正常工作,http://jsfiddle.net/jPfXS/105/ – razvai

+0

我编辑了答案和它的小提琴工作。如果您使用的是第一个/最后一个输入,我还添加了一个支持。 – Zenoo

+0

感谢队友,你是一个救星! – razvai

相关问题