jQuery Set Selected Option Using Next

后端 未结 11 1005
野趣味
野趣味 2021-02-01 01:59

How can I, using jQuery, set the \"next\" item of an already selected item as \"selected.\"

For example, if I have:


                        
    
提交评论

  • 2021-02-01 02:17
    $('#next').click( function(){
        $('#colored_background option:selected').next('option').attr('selected', 'selected');
        changeBackgroundColor();
    });
    

    Working at What is my favorite color?. Click on the arrows.

    0 讨论(0)
  • 2021-02-01 02:19
    $('option:selected', 'select').removeAttr('selected').next('option').attr('selected', 'selected');
    

    Check out working code here http://jsbin.com/ipewe/edit

    0 讨论(0)
  • 2021-02-01 02:23

    This is what i just used, i like how clean it is :-)

    $('select').val(function(){
        var nextOption = $(this).children(':selected').next();
        return $(nextOption).val();
    }).change();
    
    0 讨论(0)
  • 2021-02-01 02:26

    you can use

    $('option:selected').next('option')
    

    or

    $('option:selected + option')
    

    And set the value:

    var nextVal = $('option:selected + option').val();
    $('select').val(nextVal);
    
    0 讨论(0)
  • 提交回复
    热议问题