Prototype-form-element-present

提供:Dev Guides
移動先:案内検索

プロトタイプ-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')

           //are both fields present?
            valid = $('username').present() && $('email').present()

            if (valid) {
              //in real world we would return true here to allow the
              //form to be submitted return 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>Click the button to see the result.</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>

出力