select根据value或text动态设置选中

1.根据value设置选中

   var testValue = "ZY";

   $(".u_tag").find("option").each(function() {
        if ($(this).val() == testValue) {
            $(this).attr("selected", true);
        } else {
            $(this).attr("selected", false);
        }
   });

2.根据text设置选中 

   var testText = "增雨";   

   $(".u_tag").find("option").each(function() {
        if ($(this).text() == testText) {
            $(this).attr("selected", true);
        } else {
            $(this).attr("selected", false);
        }
   });
    

 

你可能感兴趣的:(select根据value或text动态设置选中)