开发者

display default as first option in select box without changing order

Is it possible to have a default option displayed in select box without changing the order of the other options?

<select>
    <option>
    1
    </option>
    <option>
    2
    </option>
    <option>
    3
    </option>
    <option>
    4
    </option>
    <op开发者_运维问答tion>
    5
    </option>
</select>
  1. Default 1 is displayed:

display default as first option in select box without changing order

2. I want 3 displayed as default without changing order of option list:

display default as first option in select box without changing order


try something like

$("select").val($("select option[value='fb']").val());

http://jsfiddle.net/qu5fF/

or if you are using jquery 1.6 or higher use prop

$("select option[value='fb']").prop("selected",true);

http://jsfiddle.net/qu5fF/1/

for jquery version lower then 1.6 you can use .attr

$("select option[value='fb']").attr("selected","selected");

http://jsfiddle.net/qu5fF/3/


You can add the element at the 0 index.

<script type="text/javascript">
   var elem = document.getElementById('<%= DropDown.ClientID %>');
   var myNewOption = new Option(val.Name, val.ID);
   elem .options[elem .options.length] = myNewOption;
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜