set option “selected” attribute from dynamic created option

后端 未结 17 1730
时光说笑
时光说笑 2020-11-30 20:01

I have a dynamically created select option using a javascript function. the select object is


<         


        
17条回答
  •  误落风尘
    2020-11-30 20:51

    You could search all the option values until it finds the correct one.

    var defaultVal = "Country";
    $("#select").find("option").each(function () {
    
        if ($(this).val() == defaultVal) {
    
            $(this).prop("selected", "selected");
        }
    });
    

提交回复
热议问题