當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


HTML required屬性用法及代碼示例


它是一個布爾屬性,用於指定在提交表單之前必須填寫輸入元素。

元素:此屬性可以與以下列出的三個元素相關聯:

  • <輸入>
  • <選擇>
  • <textarea>
    <輸入>:必需的屬性可以在<input>元素中使用。
  • 用法:
    <input required>
  • 示例1:
    <!DOCTYPE html> 
    <html> 
        <head> 
            <title>required Attribute</title> 
            <style> 
                h1, h2 { 
                    color:green; 
                    font-style:italic; 
                } 
                body { 
                    text-align:center; 
                } 
            </style> 
        </head> 
        <body> 
            <h1>GeeksForGeeks</h1> 
            <h2>required attribute in input Field</h2> 
            <form action=""> 
                Username:  
                <input type="text" name="usrname" required> 
                <br> 
                Password:
                <input type="password"name="password"> 
                <br> 
                <input type="submit"> 
            </form> 
        </body> 
    </html>                    
  • Output:
  • <select>:此元素還可以使用必需的屬性。

  • 用法:
    <select required>
  • 示例-2:
    <!DOCTYPE html> 
    <html> 
        <head> 
            <title>required Attribute</title> 
            <style> 
                h1, h2 { 
                    color:green; 
                    font-style:italic; 
                } 
                body { 
                    text-align:center; 
                } 
            </style> 
        </head> 
        <body> 
            <h1>GeeksForGeeks</h1> 
            <h2>required attribute in select Field</h2> 
            <form action=""> 
                <select required> 
                    <option value="">None</option> 
                    <option value="ds">Data Structure</option> 
                    <option value="algo">Algorithm</option> 
                    <option value="os">Operating System</option> 
                    <option value="cn">Computer Network</option> 
                </select> 
                <input type="submit"> 
            </form> 
        </body> 
    </html>                    
  • Output:
  • <textarea>:此元素也可以使用必需的屬性。

  • 用法:
    <textarea required>
  • 示例3:
    <!DOCTYPE html> 
    <html> 
        <head> 
            <title>required Attribute</title> 
            <style> 
                h1, h2 { 
                    color:green; 
                    font-style:italic; 
                } 
                body { 
                    text-align:center; 
                } 
            </style> 
        </head> 
        <body> 
            <h1>GeeksForGeeks</h1> 
            <h2>required attribute in Textarea Field</h2> 
            <form action=""> 
                <textarea rows="7" cols="50" 
                                 name="comment" required> 
                </textarea> 
                <input type="submit"> 
            </form> 
        </body> 
    </html>                    
  • Output:

支持的瀏覽器:必需屬性支持的瀏覽器如下:

  • 穀歌瀏覽器5.0+
  • Internet Explorer 11.0以上
  • Firefox 4+
  • Opera 9.7+




相關用法


注:本文由純淨天空篩選整理自ManasChhabra2大神的英文原創作品 HTML | required Attribute。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。