javascript高级之表单校验

    xiaoxiao2021-08-25  108

    表单校验是指:规范数据的输入格式:

    方式一:

    思路:(1)创建一个form标签,使用onsumbit事件

    (2)创建文本输入项,创建submit提交按钮,

    (3)使用绑定的第一种方式:1根据id拿到标签对象,在根据标签对象拿到该标签的value值

           2if判断为空则为false,不为空则为true.

    <html>  <head>   <title>html示例</title>  </head>  <body> <!-- 表单的提交方式一 --> <form method="get" οnsubmit="return check();"> username: <input type="text" name="username" id="username"/> <br/> password: <input type="password" name="password"/> <br/> <input type="submit" value="提交"/> </form>  </body> <script type="text/javascript"> function check(){ var a = document.getElementsByName("username")[0]; if(a.value == ""){ return false; } return true; } </script> </html>

    方式二:使用button进行表单校验

    思路:(1)创建form标签,创建输入项

    创建button按钮.关联onclick事件

    (2)事件绑定第一种方式:1根据id拿到标签对象,在根据标签对象拿到该标签的value值

    2进行非空判断,调用submit(),

    转载请注明原文地址: https://ju.6miu.com/read-677064.html

    最新回复(0)