获取有关加载提交的表单数据

时间:2016-05-05 07:20:16

标签: javascript post

我想要通过POST方法

在页面加载时提交的表单数据

这是我的代码

virtual

我想要的是在加载此页面时获取表单数据

我尝试了什么

#include<iostream>
using namespace std;
class Employee{
public:
    std::string name;
    Employee(std::string name = "Employee"):name(name){}
    void what(){
        cout<<name<<endl;
    }
};
class Secretary:public Employee{
    public:
    Secretary(std::string name = "Secretary"):Employee(name){}
};
class Manager:public Employee{
    public:
    Manager(std::string name = "Manager"):Employee(name){}
};
class Director:public Manager{
    public:
    Director(std::string name = "Director"):Manager(name){}
};
void f(Employee*);
int main(){
    Employee a;
    Manager b;
    Director c;
    Secretary d;
    f(&a);
    f(&b);
    f(&c);
    f(&d);
    return 1;
}
void f(Employee *a){
    a->what();
}

2 个答案:

答案 0 :(得分:0)

您需要在window.onload evnet中编写代码。

试试这个

window.onload = function() {    
      var str = '';
      var status, date;
      var inputs = document.getElementsByTagName('input');
      for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].name.toLowerCase() === 'status') {
        	status= inputs[i];
        }
        else if (inputs[i].name.toLowerCase() === 'date') {        	
        	date= inputs[i];
        }
      }//for close

      if (status != null) {
      	str += status.value;
      }      
      if (date != null) {      	
      	str += date.value;
      }

      alert(str);
    };
<<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
    <body onload="document.forms["Form1"].submit()">

<form id="Form1" method="POST" action="page2.aspx">

<input type="hidden" name="Status" value="success">
<input type="hidden" name="Date" value="05/05/2016">

</form>

</body>

答案 1 :(得分:0)

为什么在用户甚至没有填写 body onload 表单时提交表单! 如果您被允许使用 Jquery ,它将轻松完成您的任务

 document.getElementsByTagName('form')[0].onsubmit = function () {
$(this).serializeArray();
}//func close

请查看此link for details

这是一个小型演示

$(document).ready(
   function() {
     $("#Form1").on('submit', function() {
       var data = $(this).serializeArray()
       console.log(data);
       for (var i = 0; i < data.length; i++) {

         document.body.innerHTML += 'Key: '+data[i].name +' and Value: '+ data[i].value + '</br>';
       }

       return false;
     });

     $("#Form1").submit();
   }
 )
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="Form1" method="POST" action="page2.aspx">

  <input type="hidden" name="Status" value="success">
  <input type="hidden" name="Date" value="05/05/2016">

</form>