zoukankan      html  css  js  c++  java
  • js获取form的方法

    转自‘https://www.cnblogs.com/Sunnor/p/5293042.html’

    本文实例讲述了js获取form的方法。分享给大家供大家参考。具体如下:

    先来看下面代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <html>
    <head>
    <scirpy>
    window.onload=function(){
       var f1=document.f1;
       var f2=document.forms[1];
     alert(f2.id);
      var f3=document.forms['f1'];
    }
    </script>
    </head>
    <body>
    <form id="f1" value="f1"></from>
    <from id="f2" value="f2"></form>
    </body>
    </html>

    操作表单:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    <html>
     <head>
     <script>
      function checkform(f){
      var uname=f.username;
      var pwd=f.password;
      if(uname.value.length<4){
      alert('用户长度必须大于4');
      return false;
      }
      if(pwd.value.length!=6){
      alert('用户密码必须大于 6位');
      return false;
      }
      return true;
      }
     </script>
     </head>
     <body>
     <form id="f1" name="f1" method="post" action=""
     onsubmit="return checkform(this)">
       <input name="username" value="" /></br>
      <input name="password" value="" /></br>
      <input type="button" value="提交" /> 
     </form>
     </body>
    </html>

    js操作form的三种方式:

    1. 利用表单在文档中的索引或表单的name属性来引用表单

    复制代码 代码如下:
    document.forms[i]  //得到页面中的第i个表单
    document.forms[fromName] //得到页面中相应name的表单

    2. 利用表单的id属性

    复制代码 代码如下:
    document.getElementById(formId);

    3.

    复制代码 代码如下:
    document.formName;//最为常用的一种方式
  • 相关阅读:
    sqlserver中判断表或临时表是否存在
    Delphi 简单方法搜索定位TreeView项
    hdu 2010 水仙花数
    hdu 1061 Rightmost Digit
    hdu 2041 超级楼梯
    hdu 2012 素数判定
    hdu 1425 sort
    hdu 1071 The area
    hdu 1005 Number Sequence
    hdu 1021 Fibonacci Again
  • 原文地址:https://www.cnblogs.com/sharpest/p/7326332.html
Copyright © 2011-2022 走看看