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

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

我以为增加一个"value"属性设置为<select>元素将导致<option>包含我提供的"value"默认情况下选择:


<select name="hall" id="hall" value="3">

  <option>1</option>

  <option>2</option>

  <option>3</option>

  <option>4</option>

  <option>5</option>

</select>

然而,这并不像我所预期的那样有效。我该怎么设置<option>元素是否默认选中?



30秒到达战场
浏览 3986回答 3
3回答

冉冉说

集selected="selected"对于您希望成为默认选项的选项。<option selected="selected">3</option>

猛跑小猪

如果您希望将默认文本作为占位符/提示,但不认为是有效值(类似于“在此完成”,请“选择您的国家”ECC)。你可以这样做:<select>&nbsp;<option&nbsp;value=""&nbsp;selected&nbsp;disabled&nbsp;hidden>Choose&nbsp;here</option>&nbsp;<option&nbsp;value="1">One</option>&nbsp;<option&nbsp;value="2">Two</option>&nbsp; <option&nbsp;value="3">Three</option>&nbsp;<option&nbsp;value="4">Four</option>&nbsp;<option&nbsp;value="5">Five</option>&nbsp;</select>

qq_花开花谢_0

完整的例子:<select name="hall" id="hall">&nbsp;&nbsp; <option>&nbsp;&nbsp; &nbsp; 1&nbsp;&nbsp; </option>&nbsp;&nbsp; <option>&nbsp;&nbsp; &nbsp; 2&nbsp;&nbsp; </option>&nbsp;&nbsp; <option selected>&nbsp;&nbsp; &nbsp; 3&nbsp;&nbsp; </option>&nbsp;&nbsp; <option>&nbsp;&nbsp; &nbsp; 4&nbsp;&nbsp; </option>&nbsp;&nbsp; <option>&nbsp;&nbsp; &nbsp; 5&nbsp;&nbsp; </option>&nbsp;</select>&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP