简体   繁体   English

如何使HTML下拉选项无法点击

[英]How to make an HTML dropdown option not clickable

<form action="submit.php" method="post">
<select name="input"> 
<option value="Athletics:">Athletics:</option>
<option value="Running">Running</option>
<option value="Paragliding">Paragliding</option>
<option value="Swimming">Swimming</option>
</select> 
<input type="Submit" value="Next">
</form>

I'm trying to make "Athletics:" NOT click-able, so users have to choose their specific sport. 我试图让“田径运动:”不能点击,所以用户必须选择他们的特定运动。 Help appreciated, thanks. 帮助表示感谢,谢谢。

If you plan on adding more options to this dropdown that aren't "Athlectics", I think what you may be looking for here is actually <optgroup> : 如果您计划在此下拉列表中添加更多不是“Athlectics”的选项,我认为您在这里寻找的实际上是<optgroup>

<select>
  <option value=""></option>
  <optgroup label="Athlectics">
   <option value="Running">Running</option>
   <option value="Paragliding">Paragliding</option>
   <option value="Swimming">Swimming</option>
  </optgroup>
</select>

It looks like this : 它看起来像这样

在此输入图像描述

This is useful for categorizing groups of options in a dropdown. 这对于在下拉列表中对选项组进行分类非常有用。 "Athlectics" will not be a selectable option. “Athlectics”不是一个可选择的选择。

Otherwise I think you should just use "Athlectics" as the label for this field and remove it from the options: 否则我认为您应该使用“Athlectics”作为此字段的标签并将其从选项中删除:

<label>Athlectics: <select>...</select></label>

You should always use a label anyways for accessibility purposes, and it generally improves your UI. 您应始终使用标签以便于访问,并且通常会改善您的UI。

If you really just want to disable an option, use the disabled attribute: 如果您确实只想禁用某个选项,请使用disabled属性:

<option value="Athletics:" disabled>Athletics:</option>

Reference: 参考:

add disabled="disabled" attribute to this option 将disabled =“disabled”属性添加到此选项

<option value="Athletics:" disabled="disabled">Athletics:</option>

in some browsers it will be still on the 1st place, but 2nd option will be selected already, so you have to use javascript to make this word visible or just put in the label: 在某些浏览器中它仍然会在第一位,但是第二个选项已经被选中,所以你必须使用javascript来使这个单词可见或者只是放在标签中:

<label>Athletics:<select> ...[all other options]... </select></label>

You could do this which would not allow users to select "Alhletics" 您可以这样做,不允许用户选择“Alhletics”

<form action="submit.php" method="post">
<select name="input"> 
<option value="Athletics:" disabled>Athletics:</option>
<option value="Running">Running</option>
<option value="Paragliding">Paragliding</option>
<option value="Swimming">Swimming</option>
</select> 
<input type="Submit" value="Next">
</form>

Or you could do this and add a label 或者您可以这样做并添加标签

<form action="submit.php" method="post">
<label for="input">Athletics</label>
<select name="input"> 
<option value="Running">Running</option>
<option value="Paragliding">Paragliding</option>
<option value="Swimming">Swimming</option>
</select> 
<input type="Submit" value="Next">
</form>

Another solution work for me is: 另一个解决方案对我来说是:

<select name="input" onmousedown="function(){return false;}"> 
<option value="Athletics:">Athletics:</option>
<option value="Running">Running</option>
<option value="Paragliding">Paragliding</option>
<option value="Swimming">Swimming</option>
</select> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM