JavaScript 表单提交方式

本文介绍了如何使用JavaScript操作HTML表单元素,包括设置表单的action属性并提交表单,以及通过不同方法获取表单对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>form.html</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>

  <body>
       <form  name="form1"  action="test.html" method="post">
          <input type="text" name="username" value="zhang" />
          <input type="button" name="ok" value="保存1"/>
       </form>

       <form name="form2" action="test1.html" method="post">
          <input type="text" name="username" value="zhang2" />
          <input type="button" name="ok2" value="保存2"/>
       </form> 
  </body>  
  <script language="JavaScript">
    //使用两种方式输出表单的action值 (使用表单的名称,使用表单数组forms)
    //方法一:document.表单名
    var form1 = document.form1;
    //alert(form1.action);


    //使用两种方式输出表单的method的值(使用表单数组forms,使用表单数组forms)
    //方法二:document.forms[索引值](表单对象)
    var form2 = document.forms[1];
    alert(form2.action);
  </script> 

</html>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>form.html</title>

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>

  <body>
       <form name="form1" action="" method="post">
          患者姓名:<input type="text" name="username" value="zhang" />
          <input type="button" name="ok" value="打印患者信息" onclick="printPerson()" />
          <input type="button" name="ok" value="查询患者信息" onclick="selectPerson()" />
       </form>
  </body>

  <script language="JavaScript">
    //通过javaScript函数的方式访问printPerson.html和selectPerson.html
    function printPerson(){
        //1. 获取表单
        var form1 = document.form1;
        //2. 设置表单的action属性
        form1.action = "printPerson.html";
        //3. 提交表单
        form1.submit();
    }

    function selectPerson(){
        var form1 = document.forms[0];
        form1.action = "selectPerson.html";
        form1.submit();
    }

  </script>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值