获取条件json格式的表单数据
Get form data in conditional json format
我有一个html表单,比如:
<form action="" id="my-form">
<input type="text" name="id" value="1"/>
<input type="text" name="custom" value="a"/>
<input type="text" name="custom" value="b"/>
<input type="text" name="custom" value="c">
</form>
当我获得json格式的表单数据时,它显示为:
{id: 1, custom: c} //get the last value only
我需要这样的数据方式:
如果
name="custom"
在我的表单中只存在一次,json将是{id: 1, custom: c} // it is working now
如果
name="custom"
在我的表单中多次存在,json将是{ id: 1, multiple:[ {custom: a}, {custom: b}, ... ] }
或
{id: 1, custom:[a, b, c]}
我获取json数据的方法是:
function getFormData($form){
var unindexed_array = $form.serializeArray();
var indexed_array = {};
$.map(unindexed_array, function(n, i){
indexed_array[n['name']] = n['value'];
});
return indexed_array;
}
var $form = $("#my-form");
var data = getFormData($form);
如何解决这个问题?提前感谢。
这里有一些解决方案
<!DOCTYPE html>
<html>
<body>
<form action="" id="my-form">
<input type="text" name="id" value="1"/>
<input type="text" name="custom" value="a"/>
<input type="text" name="custom" value="b"/>
<input type="text" name="custom" value="c">
</form>
<script>
var objData = {};
var tmpArray = [];
var allInputs = document.getElementById("my-form").elements;
for (var i = 0, len = allInputs.length; i < len; i++) {
var existInArray = false;
for (var j = 0, lenArr = tmpArray.length; j < lenArr; j++) {
if (tmpArray[j] == allInputs[i].getAttribute('name')) {
existInArray = true;
}
}
if (!existInArray) {
tmpArray.push(allInputs[i].getAttribute('name'));
objData[allInputs[i].getAttribute('name')] = allInputs[i].getAttribute('value');
} else {
if (Object.prototype.toString.call(objData[allInputs[i].getAttribute('name')]) === '[object Array]') {
objData[allInputs[i].getAttribute('name')].push(allInputs[i].getAttribute('value'));
} else {
var tmpValue = objData[allInputs[i].getAttribute('name')];
objData[allInputs[i].getAttribute('name')] = [];
objData[allInputs[i].getAttribute('name')].push(tmpValue);
objData[allInputs[i].getAttribute('name')].push(allInputs[i].getAttribute('value'))
}
}
}
alert(JSON.stringify(objData));
</script>
</body>
</html>
也在Plkrhttps://plnkr.co/edit/zEid9XRXQYfxNHrXTsxu?p=preview
如果你想插入密钥多次使用这个后最后一个其他
if( Object.prototype.toString.call( objData[allInputs[i].getAttribute('name')] ) === '[object Array]' ) {
objData[allInputs[i].getAttribute('name')].multiple.push(allInputs[i].getAttribute('value'));
} else {
var tmpValue = objData[allInputs[i].getAttribute('name')];
objData[allInputs[i].getAttribute('name')] = { 'multiple' : []};
objData[allInputs[i].getAttribute('name')].multiple.push(tmpValue);
objData[allInputs[i].getAttribute('name')].multiple.push(allInputs[i].getAttribute('value'))
}
相关文章:
- 如何从pdftron webviewer获取表单数据
- JS验证ajax返回的html中的表单数据
- 在不同的文件中使用Javascript获取表单数据
- 使用Javascript读取Webstorage表单数据
- Uploadify-随机表单数据's已返回上载表单
- 如何在使用ajax提交的codeigniter中使用form_validation验证表单数据
- 在post-expressjs之后持久化表单数据
- 需要一个用于AJAX的Javascript库,其中包含多部分/表单数据
- jQuery发布并获取表单数据
- Ajax表单数据phpPOST不工作
- 如何将整个模型集附加到表单数据中,并在MVC中获得它
- 如何从javascript访问表单数据
- 如何从Angular Payments获取表单数据
- 通过javascript获取pdf文件的表单数据
- 有没有一种方法可以在jQuery.serialize()之前获取表单数据,或者与jQuery.sserialize(()
- 在 Angular2 中构建多部分/表单数据 POST 请求并验证输入类型文件
- 无法读取未定义的属性“用户名” - 通过电子邮件在 Node.js 中发送表单数据
- 使用 Ajax 获取表单数据
- 我无法从引导模式中清除表单数据
- 如何从 django 模板语言获取表单数据