jQuery Set Selected Option Using Next

后端 未结 11 1003
野趣味
野趣味 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:07
      $("select").prop("selectedIndex",$("select").prop("selectedIndex")+1)
    
    0 讨论(0)
  • 2021-02-01 02:09
    $('#my_sel').val($('#my_sel option:selected').next().val());
    $('#my_sel').val($('#my_sel option:selected').prev().val());
    
    0 讨论(0)
  • 2021-02-01 02:11

    From version 1.6.1 on, it's advisable to use the method prop for boolean attributes/properties such as selected, readonly, enabled,...

    var theValue = "whatever";
    $("#selectID").val( theValue ).prop('selected',true);
    

    For more info, please refer to to http://blog.jquery.com/2011/05/12/jquery-1-6-1-released/

    0 讨论(0)
  • 2021-02-01 02:12
    $('your_select option:selected').next('option').prop('selected', true)
    
    0 讨论(0)
  • 2021-02-01 02:16

    And if you want to specify select's ID:

    $("#nextPageLink").click(function(){
      $('#myselect option:selected').next('option').attr('selected', 'selected');
      $("#myselect").change();
    });
    

    If you click on item with id "nextPageLink", next option will be selected and onChange() event will be called. It may look like this:

    $("#myselect").change(function(){
      $('#myDivId').load(window.location.pathname,{myvalue:$("select#myselect").val()});
    });
    

    OnChange() event uses Ajax to load something into specified div.

    window.location.pathname = actual address

    OnChange() event is defined because it allowes you to change value not only using netx/prev button, but directly using standard selection. If value is changed, page does somethig automatically.

    0 讨论(0)
  • 提交回复
    热议问题