Change selected option in select2 dropdown with jquery

Please Select Fred George DaveAnd I know that I want to lớn change the dropdown so that the option with the value of "fg" is selected. How can I vị this with JQuery?





$("select>option:eq(3)").attr("selected", true);where 3 is the index of the option you want.

Bạn đang xem: Change selected option in select2 dropdown with jquery

Live Demo



$("#your_id ").attr("selected", "true"); Option #1 Option #2 Option #3 Option #4 Option #5 Option #6 Option #7
May be too late khổng lồ answer, but at least some one will get help.

You can try two options:

This is the result when you want lớn assign based on index value, where "0" is Index.

$("#mySelect").prop("selectedIndex", 0);don"t use "attr" since it is deprecated with lathử nghiệm jquery.

Xem thêm: What Is Dsee Hx Là Gì ? Công Nghệ Clearaudio+ Là Gì

When you want lớn select based on option value then choose this :

$("#mySelect").val("fg");where "fg" is the option value

This code worked for me:

$(function() $(" option").filter(function() return ($(this).text() == "Green"); //To select Green ).prop("selected", true););With this HTML select list:

Red Green Blue
I have sầu a different situation, where the drop down danh sách values are already hard coded. There are only 12 districts so the jQuery Autocomplete UI control isn"t populated by code.

The solution is much easier. Because I had lớn wade through other posts where it was assumed the control was being dynamically loaded, wasn"t finding what I needed and then finally figured it out.

So where you have HTML as below, setting the selected index is mix lượt thích this, note the -input đầu vào part, which is in addition khổng lồ the drop down id:

$("#project-locationSearch-dist-input").val("1"); District 1 2 3 4 5 6 7 8 9 10 11 12 Something else figured out about the Autocomplete control is how khổng lồ properly disable/empty it. We have 3 controls working together, 2 of them mutually exclusive:

//SPNspnDDL.combobox( select: function (sự kiện, ui) var spnVal = spnDDL.val(); //fire tìm kiếm sự kiện $("#project-locationSearch-pid-input").val(""); $("#project-locationSearch-pid-input").prop("disabled", true); pidDDL.empty(); //empty the pid menu );//get the labels so we have their tool tips to hand.//this way we don"t set id values on each labelspnDDL.siblings("label").tooltip();//PIDpidDDL.combobox( select: function (sự kiện, ui) var pidVal = pidDDL.val(); //fire search sự kiện $("#project-locationSearch-spn-input").val(""); $("#project-locationSearch-spn-input").prop("disabled", true); spnDDL.empty(); //empty the spn list );Some of this is beyond the scope of the post and I don"t know where khổng lồ put it exactly. Since this is very helpful and took some time to figure out, it"s being shared.

Und Also ... khổng lồ enable a control like this, it"s (disabled, false) & NOT (enabled, true) -- that also took a bit of time to figure out. :)

The only other thing to note, much in addition to lớn the post, is:

/*cảnh báo, when working with the jQuery Autocomplete UI control,the xxx-input control is a text input đầu vào created at the time a selectionfrom the drop down is picked. Thus, it"s created at that point in timeand its value must be picked fresh. Can"t be put inlớn a var & re-usedlượt thích the drop down menu part of the UI control. So you get spnDDL.empty()where spnDDL is a var created like var spnDDL = $("#spnDDL); But you can"tvì chưng this with the input đầu vào part of the control. Winded explanation, yes. That"s howI have sầu to lớn bởi vì my notes or 6 months from now I won"t know what a short hand note meansat all. :) */ //district $("#project-locationSearch-dist").combobox( select: function (event, ui) //enable spn và pid drop downs $("#project-locationSearch-pid-input").prop("disabled", false); $("#project-locationSearch-spn-input").prop("disabled", false); //clear them of old values pidDDL.empty(); spnDDL.empty(); //get new values GetSPNsByDistrict(districtDDL.val()); GetPIDsByDistrict(districtDDL.val()); );All shared because it took too long to lớn learn these things on the fly. Hope this is helpful.