喵-见缝插针 2010-08-19 01:10 采纳率: 83.3%
浏览 1032

如何设置 HTML select 元素的默认值?

I thought that adding a "value" attribute set on the <select> element below would cause the <option> containing my provided "value" to be selected by default:

<select name="hall" id="hall" value="3">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
</select>

However, this did not work as I had expected. How can I set which <option> element is selected by default?

转载于:https://stackoverflow.com/questions/3518002/how-can-i-set-the-default-value-for-an-html-select-element

  • 写回答

19条回答 默认 最新

  • csdnceshi62 2012-07-03 19:39
    关注

    You can do it like this:

    <select name="hall" id="hall">
        <option> 1 </option>
        <option> 2 </option>
        <option selected> 3 </option>
        <option> 4 </option>
        <option> 5 </option>
    </select> 
    

    Provide "selected" keyword inside the option tag, which you want to appear by default in your drop down list.

    Or you can also provide attribute to the option tag i.e.

    <option selected="selected">3</option>
    
    评论

报告相同问题?