在html中禁用/只读选择标记

21 html select readonly

我怎样才能准确地制作一个精选标签.我已经读过你不能将select设置为readonly.我这样做我可以将其设置为禁用但是...这不是一个选项.我希望用户在select标签中查看其余选项,但无法选择其中一个.

有任何想法吗?

Muh*_*din 34

你可以使用disabled属性.

例如.

<select >
  <option disabled="disabled" value="volvo">Volvo</option>
  <option disabled="disabled" value="saab">Saab</option>
  <option disabled="disabled" value="mercedes">Mercedes</option>
  <option disabled="disabled" value="audi">Audi</option>
</select>
Run Code Online (Sandbox Code Playgroud)


arm*_*men 11

假设标签id ="mySelectID",jQuery是常驻的,那么:

<script language="javascript">
// disable all the options that are not in use:
$("#mySelectID option").not(":selected").attr("disabled", "disabled");

// to remove readonly, enable them again:
$("#mySelectID option").not(":selected").attr("disabled", "");
</script>
Run Code Online (Sandbox Code Playgroud)