jquery selection if option.value equal something, mark a selected

前端 未结 6 757
说谎
说谎 2021-02-12 19:45

I have some question about jquery selection. In my case, how to match if the option.value equal something, mark a selected for it. Online code here

6条回答
  •  爱一瞬间的悲伤
    2021-02-12 20:12

    Your code works well. Just update 'this' with $(this). If there are no any other select dropdowns you can omit 'div#selection select.' like below.

    $(document).ready(function(){
        var num = 3;
        $("select option").each(function(){     
            if($(this).val()==num){
                $(this).attr("selected","selected");    
            }
        });
    });
    

提交回复
热议问题