How to set the option selected by jquery, i.e.

I am trying to set an option selected by its value. My code is here. It works in FF and Chrome, but not in IE. What can anyone help me?

var province = $("#hideProvince").val(); if(province != ""){ $("#province option[value='"+ province + "']").attr("selected", "selected"); } 
+4
source share
2 answers

Try attr("selected", true) instead of attr("selected", "selected") , I found this post , it may be related to what you are looking for.

 var province = $("#hideProvince").val(); if(province != ""){ $("#province option[value='"+ province + "']").attr("selected", true); } 
+1
source

You can set val() to select to update the selected option:

 var province = $("#hideProvince").val(); if (province != ""){ $("#province").val(province) } 

Script example

+2
source

Source: https://habr.com/ru/post/1444347/


All Articles