簡體   English   中英

HTML 表單中的單選按鈕無法正常工作

[英]Radio buttons in HTML form not working properly

我在 HTML 中學習 Javascript 和 forms,我知道單選按鈕。 但問題是當我編寫 3 個單選按鈕進行測試時,不知何故它不起作用,當我選中“1”(這是一個名稱)並選擇另一個像“2”時,它不會取消選中“ 1",我的瀏覽器有問題嗎? 或者我的代碼有誤,我使用的是 Microsoft Edge 瀏覽器:這是我的代碼:

 <form> <label for="html">This one is Html </label> <input type="radio" name="html" id="html" value="HTML"> <br> <label for="css">This one is Css</label> <input type="radio" name="css" id="css" value="CSS"> <br> <label for="javscript">This one is Javascript</label> <input type="radio" name="javscript" id="javascript" value="JAVASCRIPT"> </form>

任何人都知道我的問題是什么?

name屬性作為一個組。 因此,如果您在單選按鈕上設置相同的name ,它們將在同一組中。

 <form> <label for="html">This one is Html </label> <input type="radio" name="group1" id="html" value="HTML"> <br> <label for="css">This one is Css</label> <input type="radio" name="group1" id="css" value="CSS"> <br> <label for="javscript">This one is Javascript</label> <input type="radio" name="group1" id="javascript" value="JAVASCRIPT"> </form>

這很簡單,你只需要為每個組使用相同的name ,這里我使用codetype

 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width. initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="style.css"> </head> <body> <form> <label for="html">This one is Html </label> <input type="radio" name="codetype" id="html" value="HTML"> <br> <label for="css">This one is Css</label> <input type="radio" name="codetype" id="css" value="CSS"> <br> <label for="javscript">This one is Javascript</label> <input type="radio" name="codetype" id="javascript" value="JAVASCRIPT"> </form> </body> </html>

請您可以使用單選按鈕,當時所有單選按鈕name設置相同,然后您 select 當時一個單選按鈕。

 <form> <label for="html">This one is Html </label> <input type="radio" name="skill" id="html" value="HTML"> <br> <label for="css">This one is Css</label> <input type="radio" name="skill" id="css" value="CSS"> <br> <label for="javscript">This one is Javascript</label> <input type="radio" name="skill" id="javascript" value="JAVASCRIPT"> </form>

只需為每個組使用相同的名稱屬性值。

 <form> <label for="html">This one is Html </label> <input type="radio" name="radio_button" id="html" value="HTML"> <br> <label for="css">This one is Css</label> <input type="radio" name="radio_button" id="css" value="CSS"> <br> <label for="javscript">This one is Javascript</label> <input type="radio" name="radio_button" id="javascript" value="JAVASCRIPT"> </form>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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