如果我们想为单选按钮分配一个选中的初始值,我们可以使用 HTML checked属性。
在下面的示例中,我们使用 <form> 标记并其中添加 <div> 元素。
在我们的 <div> 元素中,我们添加 <input> 和 <label> 标签,然后对我们希望最初检查的 <input> 使用 checked="checked" 。
使用 checked="checked" 分配选中值的示例:
<!DOCTYPE html> <html> <head> <title>文档的标题</title> </head> <body> <p>Which is your favorite fruit?</p> <form action="/form/submit" method="get"> <div> <input type="radio" id="apple" name="fruite" value="apple" checked="checked"> <label for="apple">Apple</label> </div> <div> <input type="radio" id="banana" name="fruite" value="banana"> <label for="banana">Banana</label> </div> <br/> <input type="submit" value="Submit" /> </form> </body> </html>
或者我们可以只使用没有任何值的 checked 属性,如下所示。
使用没有值的检查属性分配检查值的示例:
<!DOCTYPE html> <html> <head> <title>文档的标题</title> </head> <body> <p>Which is your favorite fruit?</p> <form action="/form/submit" method="get"> <div> <input type="radio" id="pineapple" name="fruite" value="pineapple"> <label for="pineapple">Pineapple</label> </div> <div> <input type="radio" id="orange" name="fruite" value="orange" checked> <label for="orange">Orange</label> </div> <div> <input type="radio" id="kiwi" name="fruite" value="kiwi"> <label for="kiwi">Kiwi</label> </div> <div> <input type="radio" id="mango" name="fruite" value="mango"> <label for="mango">Mango</label> </div> <br/> <input type="submit" value="Submit" /> </form> </body> </html>
日期:2020-06-02 22:15:05 来源:oir作者:oir