Set the selected index of a Dropdown using jQuery

后端 未结 10 799
离开以前
离开以前 2020-11-30 17:49

How do I set the index of a dropdown in jQuery if the way I\'m finding the control is as follows:

$(\"*[id$=\'\" + originalId + \"\']\") 

I

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 18:21

    Just found this, it works for me and I personally find it easier to read.

    This will set the actual index just like gnarf's answer number 3 option.

    // sets selected index of a select box the actual index of 0 
    $("select#elem").attr('selectedIndex', 0);
    

    This didn't used to work but does now... see bug: http://dev.jquery.com/ticket/1474

    Addendum

    As recommended in the comments use :

    $("select#elem").prop('selectedIndex', 0);

提交回复
热议问题