我想获取html dom属性并在提交表单时变成JSON。
这是html表单元素:
而JSON是这样的:
[{ { "question": "1、question1?", "ask": { ["type": "checkbox", "name": "yes", "value": "yes", "isChecked": 0], ["type": "checkbox", "name": "no", "value": "no", "isChecked": 1] } },{ "question": "2、question2?", "ask": { ["type": "checkbox", "name": "below_60", "value": "60", "isChecked": 1], ["type": "checkbox", "name": "btw_60_80", "value": "60-80", "isChecked": 1], ["type": "checkbox", "name": "btw_80_100", "value": "80-100", "isChecked": 1] } } }]
试试这个:
$(function(){ var data = []; $('fieldset').each(function(){ var fieldset = $(this); var item = { question: fieldset.find('legend').text(), ask: [] }; fieldset.find('input').each(function(){ var input = $(this); item.ask.push({ type: input.attr('type'), name: input.attr('name'), isChecked: input.is(':checked') ? '1' : '0', value: fieldset.find('[for="' + input.attr('id') + '"]').text() }); }); data.push(item); }); console.log(data); });
演示: http : //jsfiddle.net/Qf3pX/
看起来像https://api.jquery.com/serialize/可能会帮助你。
以上就是jQuery教程分享如何使用jquery每个HTML dom和get属性变成JSON相关内容,想了解更多jQuery开发(异常处理)及jQuery教程关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/jquerytutorial/548107.html