Set the selected index of a Dropdown using jQuery

后端 未结 10 793
离开以前
离开以前 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:40
    $("[id$='" + originalId + "']").val("0 index value");
    

    will set it to 0

    0 讨论(0)
  • 2020-11-30 18:42

    Select 4th option

    $('#select').val($('#select option').eq(3).val());
    

    example on jsfiddle

    0 讨论(0)
  • 2020-11-30 18:44

    I'm writing this answer in 2015, and for some reason (probably older versions of jQuery) none of the other answers have worked for me. I mean, they change the selected index, but it doesn't actually reflect on the actual dropdown.

    Here is another way to change the index, and actually have it reflect in the dropdown:

    $('#mydropdown').val('first').change();
    
    0 讨论(0)
  • 2020-11-30 18:45

    You want to grab the value of the first option in the select element.

    $("*[id$='" + originalId + "']").val($("*[id$='" + originalId + "'] option:first").attr('value'));
    
    0 讨论(0)
提交回复
热议问题