How can I set the default value for an HTML <select> element?

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?


selected="selected"为您想要成为默认选项的选项。

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

如果你想让一个默认文本作为一种占位符/提示,但不被认为是一个有效的值(类似“完成这里”,“选择你的国家”等),你可以这样做:

<select>
  <option value="" selected disabled hidden>Choose here</option>
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
  <option value="4">Four</option>
  <option value="5">Five</option>
</select>

完整的例子:

<select name="hall" id="hall"> 
  <option> 
    1 
  </option> 
  <option> 
    2 
  </option> 
  <option selected> 
    3 
  </option> 
  <option> 
    4 
  </option> 
  <option> 
    5 
  </option> 
</select> 
链接地址: http://www.djcxy.com/p/2534.html

上一篇: 应该不惜任何代价在HTML中避免表格?

下一篇: 我如何设置HTML <select>元素的默认值?