繁体   English   中英

将下一个和上一个附加到无线电按钮

[英]Append NEXT and PREVIOUS to RADIO BUTTON

我已经坚持了几天的代码,而我真正想做的是在单选按钮上附加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>  

我要在此处执行的操作是,当我单击“下一步”时,它将选择其下方的单选按钮,因此上面的代码具有bundle-73,bundle-72和bundle-74,表示如果选择的默认值为bundle- 73,当我单击下一步时,它应该选择bundle-72。

您的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/

假设您只有一组单选按钮。

您应该可以自己整理上一个动作。

参考文献:

也许像这样吗?

<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;
});
<!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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM