jquery设置select下拉框默认选中

<select id="selectId">
  <option value="orange">橙色</option>
  <option value="yellow">黄色</option>
</select>

 1、通过value值设置

$("#selectId").val("yellow");

 2、通过prop设置

$("#selectId option[value='yellow']").prop("selected",true);
$("#selectId option[value='yellow']").prop("selected","selected");

3、通过循环遍历匹配

//通过循环遍历匹配text
var color = "黄色";
$("#selectId").find("option").each(function(){
    if($(this).text() == color)    {
        $(this).attr("selected",true);
    }
});
//通过遍历匹配value
var color = "yellow";
$("#selectId").find("option").each(function(){
    if($(this).val() == color)    {
        $(this).attr("selected",true);
    }
}); 

 

posted @ 2021-07-01 19:10  一隅桥畔  阅读(3431)  评论(0)    收藏  举报