Prototype - Form.Element present() 方法

如果文本输入有内容,则此方法返回 true,否则返回 false。

语法

formElement.present();

返回值

如果文本输入有内容,则返回 true,否则返回 false。

示例

尝试先提交以下表单而不填写信息,然后在字段中输入一些文本后再次提交 −

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showResult() {
            var valid, msg = $('msg')

            // 两个字段都存在吗?
            valid = $('username').present() && $('email').present()

            if (valid) {
               // 在现实世界中,我们将在这里返回 true 以允许
               // 提交的表单返回 true
               msg.update('Passed validation!').style.color = 'green'
            } else {
               msg.update('Fill out all the fields.').style.color = 'red'
            }
            return false
         }
      </script>
   </head>

   <body>
      <p>单击按钮查看结果。</p>
      <br />

      <form id = "example" action = "#">
         <fieldset>
            <legend>User Details</legend>
            <p style = "color:green;" id = "msg">
               Fill out all the fields:
            </p>
            <div>
               <label for = "username">Username</label>
               <input id = "username" name = "username" type = "text">
            </div>
            <div>
               <label for = "email">Email Address</label>
               <input id = "email" name = "email" type = "text">
            </div>
            <div>
               <input value = "result" type = "button" onclick = "showResult()";>
            </div>
         </fieldset>
      </form>
      
   </body>
</html>

输出

prototype_form_management.html