Добавить СЛЕДУЮЩУЮ и ПРЕДЫДУЩУЮ К КНОПКЕ РАДИО - PullRequest
0 голосов
/ 21 сентября 2011

Я застрял с кодом в течение нескольких дней, и что я действительно пытаюсь сделать, это добавить NEXT и PREVIOUS (ссылка / кнопка) на переключатель.

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

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

Что я пытаюсь сделать здесь, так это то, что, когда я нажимаю «Далее», он выбирает переключатель под ним, поэтому в приведенном выше коде есть «Бандл-73», «Бандл-72» и «Бандл-74», которые говорят, что выбран по умолчанию. это bundle-73, когда я нажимаю дальше, он должен выбрать bundle-72.

Ответы [ 3 ]

1 голос
/ 21 сентября 2011

Ваш HTML выглядит неработающим, поэтому я предполагаю, что вы действительно имеете в виду следующее:

<div id="slides">
    <ul class="options-list">
        <li><input type="radio" class="getradiotomove" id="bundle-73" name="bundle_option" 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>
    </ul>
</div>

Тогда ваше следующее действие будет выглядеть так:

$('#next').click(function() {
    var $all     = $('.getradiotomove');          // Get them all
    var $current = $('.getradiotomove:checked');  // Get the current one

    var index = $all.index($current) + 1;         // Find the index of the next one
    if(index >= $all.length)                      // Wrap around at the end
        index = 0;
    $($all[index]).attr('checked', 'checked');    // Check it.
});

Демонстрация: http://jsfiddle.net/ambiguous/hTgv3/1/

Предполагается, что у вас есть только одна группа переключателей.

Вы сможете самостоятельно разобраться с предыдущим действием.

Ссылки:

0 голосов
/ 21 сентября 2011
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
 <html lang="es">
   <head>
     <title>Ejemplo</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
       <script type="text/javascript">
           $(document).ready(function() {
                $('#next').click(function() {
                        var checkedOp = $('input[name=bundle_option]:checked')
                        var match = checkedOp.next('input[name=bundle_option]');
                        if (match.length > 0)
                            checkedOp.removeAttr('checked').next('input[name=bundle_option]').attr('checked', 'checked');
                    });
                $('#prev').click(function() {
                        var checkedOp = $('input[name=bundle_option]:checked');
                        var match = checkedOp.prev('input[name=bundle_option]');
                        if (match.length > 0)
                            checkedOp.removeAttr('checked').prev('input[name=bundle_option]').attr('checked', 'checked');
                });
                }                   
            );

       </script>
   </head>
   <body>
  <div id="slides">
            <input type="radio" class="getradiotomove" id="bundle-73" name="bundle_option" value="73" checked="checked">
            <input type="radio" class="getradiotomove" id="bundle-72" name="bundle_option" value="72">
            <input type="radio" class="getradiotomove" id="bundle-74" name="bundle_option" value="74">
    </div>
    <div id="buttons">
        <a href="#" id="prev">prev</a>
        <a href="#" id="next">next</a>
        <div class="clear"></div>
    </div>  
   </body>
 </html>
0 голосов
/ 21 сентября 2011

Может быть, что-то вроде этого?

<ul class="options-list">
    <li>
        <input type="radio" name="bundle_option" value="73">
    </li>
    <li>
        <input type="radio" name="bundle_option" value="72">
    </li>
    <li>
        <input type="radio" name="bundle_option" value="74">
    </li>
</ul>

и javascript:

$('#next').click(function(){
    $('[name=bundle_option]:checked').parent().next().children('[name=bundle_option]').attr('checked', true);
    return false;
});
...