原型 - 表单元素呈现()方法
此方法如果文本输入有内容,则返回 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>
输出
prototype_form_management.htm
广告