![](/img/trans.png)
[英]how to detect when a user clicks on an option in a drop down menu (select tag)
[英]How do I make the “select one” option not go through when the user clicks submit on the select tag?
在我的表單上,我試圖使選擇框顯示標准的“選擇一個”選項,以便用戶可以單擊它,然后在我已選擇的3個選項中進行選擇。 我希望表單的這一部分是必需的,同時也不允許默認選項通過。 任何使第一個選項不起作用的幫助將不勝感激! 謝謝!
<option selected="selected">Select one</option>
<option value="air">On the air</option>
<option value="web">On the web</option>
<option value="both">Both</option>
只是改變
<select required>
<option value="" selected>Select one</option>
<option value="air">On the air</option>
<option value="web">On the web</option>
<option value="both">Both</option>
</select>
用戶永遠不能提交默認選項。
您可以像這樣selected
和disabled
“ Select one
選項。
function validate(self) { console.log(self.value); }
<select onchange="validate(this)" required> <option disabled selected>Select one</option> <option value="air">On the air</option> <option value="web">On the web</option> <option value="both">Both</option> </select>
onchange
事件。 selected
標簽確保其為默認選項。 disabled
標記確保不能將其選擇為實際選項。 required
標記可阻止表單提交。 “選擇一個”文本實際上不應該是一個選項,而應該是一個標簽
<label>Select One:</label> <select> <option value="air">On the air</option> <option value="web">On the web</option> <option value="both">Both</option> </select>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.