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

前端 未结 6 774
说谎
说谎 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条回答
  •  Happy的楠姐
    2021-02-12 20:19

    You have a typo and to set the option as selected you should use prop() and not attr(). in any case you could do

    var num = 3;
    $("div#selection select.select option[value="+num+"]").prop("selected", true);
    

    fiddle here http://jsfiddle.net/YRBrp/

    EDIT - the typo of course is what Tim S. pointed out, you should use $(this).val() and not (this).val()

提交回复
热议问题