当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。