HTMLFormElement获取表单里面所有的值然后以json形式返回
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形式返回的更多相关文章
- request.getParameterMap() 获取表单提交的键值对 并且 也能获取动态表单的key
Map<String,String[]> map = request.getParameterMap();Set<String> keys = map.keySet(); 获取 ...
- 异步发送表单数据到JavaBean,并响应JSON文本返回
1) 提交表单后,将JavaBean信息以JSON文本形式返回到浏览器 <form> 编号:<input type="text" name="id&q ...
- jQuery获取表单各元素的值
radio值获取 $("input[type='radio']:checked").val(); 2,设置指定的项为当前选中项 $("input[type='radio' ...
- 21SpringMvc_异步发送表单数据到Bean,并响应JSON文本返回(这篇可能是最重要的一篇了)
这篇文章实现三个功能:1.在jsp页面点击一个按钮,然后跳转到Action,在Action中把Emp(int id ,String salary,Data data)这个实体变成JSON格式返回到页面 ...
- jQuery—获取表单标签的数据值
获取设置input标签的值 <input class="form-control" type="text" id="username" ...
- asp.net 获取表单中控件的值
原文:https://blog.csdn.net/happymagic/article/details/8480235 C# 后台获取前台 input 文本框值.(都是以控件的Name来获取) s ...
- 用jQuery获取表单的值
在日常开发过程中,有许多用到表单的地方.比如登录,注册,比如支付,填写订单,比如后台管理等等. 使用jQuery来获取表单的值是比较常见的做法. 常见表单 单行文字域:<input type=' ...
- jQuery name属性与checked属性结合获取表单元素值
var paytype = $("input[name='paytype']:checked").val(); alert(paytype); input元素下名称为paytype ...
- javascript获取表单的各项值
何谓表单? 表单是html页面中负责数据采集功能的部件,它往往由三个部分组成: 表单标签:<form></form> 用于声明表单的范围,位于表单标签中的元素将被提交.属性有m ...
随机推荐
- 浅谈WebService之JAX-RS与spring整合
背景:首先谈一下webservice: 1.Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序, 可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发 ...
- 简单来看看JavaBean
1.什么是JavaBean? JavaBean是一个遵循特定写法的java类. 用作JavaBean的类必须有一个公共的,无参数的构造方法. JavaBean的属性与普通的Java类的属性的概念一样, ...
- Nginx 503错误总结
nginx 503错误(Service Temporarily Unavailable 服务暂时不可用): 503是一种HTTP状态码,由于临时的服务器维护或者过载,服务器当前无法处理请求.这个状况 ...
- Windows安装配置xampp
建议大家直接看原文 1.安装XAMPP 进入https://www.apachefriends.org/zh_cn/index.html页面下载XAMPP 2 3.打开xampp控制版 4.修改apa ...
- Js窗口嵌套
<script type="text/javascript"> if (window.parent.window != window) { window.top.loc ...
- 接口调用 读取图片报错 Access to the path '' is denied.解决方案
调用接口 读取服务器上 图片 报错: Server was unable to process request. ---> Access to the path '图片路径' is denied ...
- vs2015 web项目加载失败解决办法
1.问题 ---------------------------Microsoft Visual Studio---------------------------Web 应用程序项目 XXWeb 已 ...
- eayui grid 每一页的行号都是从1开始
问题背景: easyui 需要显示行号的时候,我们只需要设置 rownumbers: true, 但是 不管是在哪一页,行号都是从1开始,不能连续 我们在分页的 onSelectPage 函数里去执 ...
- Visual Studio Code使用Open In Browser打开的是记事本
今天在家里学习前端开发,发现Visual Studio Code使用Open In Browser插件快速打开浏览器有问题,打开的是操作系统的记事本. 后来发现电脑的html文件默打开方式被改成了记事 ...
- ES6入门——函数的扩展
1.函数参数的默认值 在ES6之前,不能直接为函数的参数指定默认值,只能采用变通的方法.现在ES6可以为函数的参数添加默认值,简洁了许多. ES5 function show(a,b){ b = b ...