Choose option from select list using next/previous button with jquery

前端 未结 4 644
礼貌的吻别
礼貌的吻别 2021-01-20 00:48

What is best way to navigate select option using next/previous button with jquery? I have a select list when change also changes some div. Instead of just a dropdown, I want

4条回答
  •  清歌不尽
    2021-01-20 01:21

    Something like this should work -

    $("#next").click(function() {
        $("#mycars").val($("#mycars > option:selected").next().val());
    })
    
    $("#prev").click(function() {
        $("#mycars").val($("#mycars > option:selected").prev().val());
    })
    

    Interestingly this will "wrap-around" (go back to the 'Volvo' option after 'Audi') when using the next button but stop at the 'Volvo' option when using the 'Previous' button. Here's a demo -

    http://jsfiddle.net/aQSSG/

提交回复
热议问题