簡體   English   中英

當用戶單擊選擇標記上的提交時,如何使“選擇一個”選項不通過?

[英]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>

用戶永遠不能提交默認選項。

您可以像這樣selecteddisabledSelect 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM