默认情况下,组合框选择最后一个选项

Bun*_*ong 12 html jquery html5

我想要一个组合框默认选择最后一个选项(使用jquery):

<select>
    <option>item1</option>
    <option>item2</option>
    <option>item3</option>
    <option>item4</option>
    <option>item5</option>
</select>
Run Code Online (Sandbox Code Playgroud)

小智 33

做这样的事情:

$(function() {
    $("select option:last").attr("selected", "selected");
});
Run Code Online (Sandbox Code Playgroud)


Ori*_*iol 11

简单的JavaScript解决方案:

select.selectedIndex = select.options.length-1;
Run Code Online (Sandbox Code Playgroud)

演示