2014-03-27 75 views
1

我正在制作一种幻灯片形式,并且当用户单击下一张图像幻灯片时,还必须选择单选按钮。我得到了滑动工作,'下一个按钮'也在工作,但我有点卡住'prev'按钮。不明白为什么它不起作用。选择下一个/上一个单选按钮与外部按钮

fiddle

这是我走到这一步,HTML:

<form> 
<div> 
    <div class="button prev">&#60;</div> 
    <ul> 
     <li> 
      <input type="radio" id="choise_1" name="first_choise" checked="checked"/> 
      <label for="choise_1">One</label> 
     </li> 
     <li> 
      <input type="radio" id="choise_2" name="first_choise"/> 
      <label for="choise_2">Two</label> 
     </li> 
     <li> 
      <input type="radio" id="choise_3" name="first_choise"/> 
      <label for="choise_3">Three</label> 
     </li> 
    </ul> 
    <div class="button next">&#62;</div> 
</div> 
<div> 
    <div class="button prev">&#60;</div> 
    <ul> 
     <li> 
      <input type="radio" id="choise_1" name="second_choise" checked="checked"/> 
      <label for="choise_1">One</label> 
     </li> 
     <li> 
      <input type="radio" id="choise_2" name="second_choise"/> 
      <label for="choise_2">Two</label> 
     </li> 
     <li> 
      <input type="radio" id="choise_3" name="second_choise"/> 
      <label for="choise_3">Three</label> 
     </li> 
    </ul> 
    <div class="button next">&#62;</div> 
</div></form> 

的jQuery:

$(document).ready(function(){ 
$('.prev').click(function(){ 
    $(this).next().find('input:checked').parent().prev().children('input').attr("checked", true); 
    $(this).next().find('input:checked').last().removeAttr("checked"); 
}); 

$('.next').click(function(){ 
    $(this).prev().find('input:checked').parent().next().children('input').attr("checked", true); 
    $(this).prev().find('input:checked').eq(0).parent().prev().children('input').removeAttr("checked"); 
});}); 
+0

创建小提琴工作,现在你的问题是什么? –

回答

3

它可以简化为

$(document).ready(function(){ 
    $('.prev').click(function(){ 
     $(this).parent().find('input:checked').parent().prev().children('input').prop("checked", true); 
    }); 

    $('.next').click(function(){ 
     $(this).parent().find('input:checked').parent().next().children('input').prop("checked", true); 
    }); 
}); 

演示:Fiddle


同样使用:有选择器

$(document).ready(function(){ 
    $('.prev').click(function(){ 
     $(this).parent().find('li:has(input:checked)').prev().children('input').prop("checked", true); 
    }); 

    $('.next').click(function(){ 
     $(this).parent().find('li:has(input:checked)').next().children('input').prop("checked", true); 
    }); 
}); 

演示:Fiddle

+0

英雄,这是工作!谢谢!尽可能接受你的回答。 –

+0

虽然它在工作,但是当我查看浏览器中的代码时,第一个输入标签会保持检查属性。这会影响结果吗? –

+1

@PepijnGieles不,它不 –

1

可以使用.prop()代替.attr(),您可以简化为这样:

$(document).ready(function(){ 
    $('.prev').click(function(){ 
     $(this).next().find(':checked').parent().prev().find('input').prop("checked", true); 
    }); 

    $('.next').click(function(){ 
     $(this).prev().find(':checked').parent().next().find('input').prop("checked", true); 
    }); 
}); 
+0

在视觉上它正在工作,但是当我在浏览器中查找代码时,第一个输入标记不断检查属性..这会影响结果吗? –

+1

哦!看看您是否在浏览器中看到页面源代码,然后会显示页面加载时可用的默认标记。所以在页面加载的时候你的第一个输入被检查过,所以你可以在源代码中看到,但你必须使用firebug/chromeinspector来查看更改。 – Jai

相关问题