function HTMLFormElement(){
this.init();
return this.json;
}
HTMLFormElement.prototype.init = function(){
this.json = {};
this.inputs = document.querySelectorAll("input");
this.texts = document.querySelectorAll("textarea");
this.sels = document.querySelectorAll("select");
this.types = ['checkbox', 'color', 'date', 'datetime', 'datetime-local', 'month', 'week', 'time', 'email', 'file', 'hidden', 'number', 'password', 'radio', 'range', 'search', 'tel', 'text', 'url'];
this.SerializedJson();
};
HTMLFormElement.prototype.SerializedJson = function() {
if (this.inputs.length > 0 || this.texts.length > 0) {
this.getInputsValue();
this.getTextValue();
this.getSelsValue();
}
};
HTMLFormElement.prototype.getInputsValue = function(){
var input; for (var i = 0; i < this.inputs.length; i++) {
input = this.inputs[i];
var name = input.getAttribute("name");
var type = input.getAttribute("type"); if (type && name && this.types.indexOf(type.toLowerCase()) > -1) {
if (type != 'checkbox' && type != 'radio') {
this.json[name] = input.value;
} else if (type == 'radio') {
if (!this.json[name]) {
this.json[name] = '';
}
if (input.checked) {
this.json[name] = input.value;
}
} else if (type == 'checkbox') {
if (!this.json[name]) {
this.json[name] = '';
}
if (input.checked) { if (this.json[name]) {
this.json[name] += "," + input.value
} else {
this.json[name] = input.value;
}
}
}
} } }
HTMLFormElement.prototype.getTextValue = function(){
for (var i = 0; i < this.texts.length; i++) {
input = this.texts[i];
var name = input.getAttribute("name");
if (name) {
this.json[name] = input.value;
}
}; };
HTMLFormElement.prototype.getSelsValue = function(){
for (var i = 0; i < this.sels.length; i++) {
input = this.sels[i];
var name = input.getAttribute("name");
if (name) {
this.json[name] = input.value;
}
}
return this.json;
}

  使用方法:

   new HTMLFormElement());

HTMLFormElement获取表单里面所有的值然后以json形式返回的更多相关文章

  1. request.getParameterMap() 获取表单提交的键值对 并且 也能获取动态表单的key

    Map<String,String[]> map = request.getParameterMap();Set<String> keys = map.keySet(); 获取 ...

  2. 异步发送表单数据到JavaBean,并响应JSON文本返回

    1)  提交表单后,将JavaBean信息以JSON文本形式返回到浏览器 <form> 编号:<input type="text" name="id&q ...

  3. jQuery获取表单各元素的值

    radio值获取 $("input[type='radio']:checked").val(); 2,设置指定的项为当前选中项 $("input[type='radio' ...

  4. 21SpringMvc_异步发送表单数据到Bean,并响应JSON文本返回(这篇可能是最重要的一篇了)

    这篇文章实现三个功能:1.在jsp页面点击一个按钮,然后跳转到Action,在Action中把Emp(int id ,String salary,Data data)这个实体变成JSON格式返回到页面 ...

  5. jQuery—获取表单标签的数据值

    获取设置input标签的值 <input class="form-control" type="text" id="username" ...

  6. asp.net 获取表单中控件的值

    原文:https://blog.csdn.net/happymagic/article/details/8480235   C# 后台获取前台 input 文本框值.(都是以控件的Name来获取) s ...

  7. 用jQuery获取表单的值

    在日常开发过程中,有许多用到表单的地方.比如登录,注册,比如支付,填写订单,比如后台管理等等. 使用jQuery来获取表单的值是比较常见的做法. 常见表单 单行文字域:<input type=' ...

  8. jQuery name属性与checked属性结合获取表单元素值

    var paytype = $("input[name='paytype']:checked").val(); alert(paytype); input元素下名称为paytype ...

  9. javascript获取表单的各项值

    何谓表单? 表单是html页面中负责数据采集功能的部件,它往往由三个部分组成: 表单标签:<form></form> 用于声明表单的范围,位于表单标签中的元素将被提交.属性有m ...

随机推荐

  1. 使用 typeof 来检测对象是否undefined

    需求 判断是否为undefined 解决 使用 typeof 来检测对象是否已定义: if (typeof Obj !== "undefined" && Obj ! ...

  2. shodan在渗透测试中的应用

    场景1:想搜索美国所有的elasticsearch服务器 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.设计 ...

  3. 百度富文本Ueditor将图片存在项目外路径并回显

    我的毕设中需要一个类似新闻发布的功能,使用到百度富文本编辑器,不过百度富文本编辑器有点坑(只是我太菜了),粘贴图片和回显这个坑坑了我两天时间.效果是这样的: 就是可以在文本中粘贴图片并显示出来,直接说 ...

  4. 虚拟机下centos时间不正确的方便解决方法

    就是用NTP了,通过外部的服务同步时间. ntpdate us.pool.ntp.org | logger -t NTP 如果没有ntpdate ,可以使用 yum install ntpdate 进 ...

  5. C10K问题摘要

    本文的内容是下面几篇文章阅读后的内容摘要: http://www.kegel.com/c10k.html (英文版) http://www.oschina.net/translate/c10k (中文 ...

  6. 《深入理解Java内存模型》读书总结(转-总结很好)

    概要 文章是<深入理解Java内容模型>读书笔记,该书总共包括了3部分的知识. 第1部分,基本概念 包括“并发.同步.主内存.本地内存.重排序.内存屏障.happens before规则. ...

  7. flask-login的简单实用

    # encoding: utf-8 from flask import Flask, Blueprint from flask_login import (LoginManager, login_re ...

  8. java 反射实现2个int变量值的交换

    import java.io.*;import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; ...

  9. C语言转置矩阵算法

    对一个nxn阶的矩阵进行转置,算法如下: #include <stdio.h> #define n 3 void MM(int a[][n]) { int i,j,temp; ;i < ...

  10. postgres备份数据库

    1. psql --help psql is the PostgreSQL interactive terminal. Usage: psql [OPTION]... [DBNAME [USERNAM ...