我想基于html元素
创建这个JSON {
"AppName": "ERP",
"ModuleDesc": [
{
"Name": "Payroll",
"AcCESSRIGHTS": [
{
"Create": "Y",
"Retrive": "Y",
"Update": "Y",
"Delete": "Y"
}
]
},
{
"Name": "Reports",
"AcCESSRIGHTS": [
{
"Create": "Y",
"Retrive": "N",
"Update": "Y",
"Delete": "N"
}
]
},
{
"Name": "Forms",
"AcCESSRIGHTS": [
{
"Create": "Y",
"Retrive": "Y",
"Update": "N",
"Delete": "N"
}
]
}
]
}
我的HTML是:=
<table>
<tr>
<td style="width: 200px;">APPLICATION CATEGORY :</td>
<td>
<input id="Text1" maxlength="35" name="ONEICAPP" style="width: 400px; text-transform: uppercase"
type="text" value="INTERIOR SYSTEM" />
</td>
</tr>
</table>
<table>
<tr>
<td style="width: 400px;"> MODULE DESCRIPTION</td>
<td style="width: 50px;"> CREATE</td>
<td style="width: 50px;"> RETRIVE</td>
<td style="width: 50px;"> UPDATE</td>
<td style="width: 50px;"> DELETE</td>
</tr>
</table>
<div id="ModuleDescriptions">
<table >
<tr>
<td style="width: 400px;">
<input id="ModuleName1" maxlength="35" style="width: 400px; text-transform: uppercase" type="text" value="ALL REPORTS" />
</td><td>
<table ><tr>
<td style="width: 50px;"><input id="CRT-1" type="checkbox" name="ModuleName1" value="C" />
</td>
<td style="width: 50px;"><input id="RTV-1" type="checkbox" name="ModuleName1" value="R"/>
</td>
<td style="width: 50px;"><input id="UPD-1" type="checkbox" name="ModuleName1" value="U" />
</td>
<td style="width: 50px;"><input id="DLT-1" type="checkbox" name="ModuleName1" value="D" />
</td></tr></table></td>
</tr>
<tr>
<td style="width: 400px;">
<input id="ModuleName2" maxlength="35" style="width: 400px; text-transform: uppercase" type="text" value="CREATE MASTER" />
</td><td><table><tr><td style="width: 50px;"><input id="CRT-2" class="Rights" type="checkbox" name="ModuleName2" value="C" />
</td>
<td style="width: 50px;"><input id="RTV-2" class="Rights" type="checkbox" name="ModuleName2" value="R"/>
</td>
<td style="width: 50px;"><input id="UPD-2" class="Rights" type="checkbox" name="ModuleName2" value="U" />
</td>
<td style="width: 50px;"><input id="DLT-2" class="Rights" type="checkbox" name="ModuleName2" value="D" />
</td> </tr></table></td>
</tr>
<tr>
<td style="width: 400px;">
<input id="ModuleName3" maxlength="35" style="width: 400px; text-transform: uppercase" type="text" value="PROCESS" />
</td><td><table><tr><td style="width: 50px;"><input id="CRT-3" class="Rights" type="checkbox" name="ModuleName3" value="C" />
</td>
<td style="width: 50px;"><input id="RTV-3" class="Rights" type="checkbox" name="ModuleName3" value="R"/>
</td>
<td style="width: 50px;"><input id="UPD-3" class="Rights" type="checkbox" name="ModuleName3" value="U" />
</td>
<td style="width: 50px;"><input id="DLT-3" class="Rights" type="checkbox" name="ModuleName3" value="D" />
</td>
</tr></table></td>
</tr>
</table>
输入ID和相应的访问权限(复选框)名称相同 我可以使用Jquery .each循环并动态构建json字符串吗?或任何其他方式..请帮助我
答案 0 :(得分:0)
data = "{'REQTYPE':'" + Req_Type + "', 'APPNAME':'" + $('[id$=hfONEICAPPLabel]').val() + "', "
+ "'MODULEDESC': [";
$('input[id*=ModuleName]').each(function () {
j++;
CompareName = $(this).attr('id');
data = data + "{'Name':'" + $(this).val() + "', "
+ "'ACCESSRIGHTS': [{";
$("input[name='" + CompareName + "']").each(function () {
if ($(this).is(':checked')) {
if ($(this).attr('id').substring(0, 3) === "CRT") {
data = data + "'Create':'Y',";
}
else if ($(this).attr('id').substring(0, 3) === "RTV") {
data = data + "'Retrive':'Y',";
}
else if ($(this).attr('id').substring(0, 3) === "UPD") {
data = data + "'Update':'Y',";
}
else if ($(this).attr('id').substring(0, 3) === "DLT") {
data = data + "'Delete':'Y'";
}
}
else {
if ($(this).attr('id').substring(0, 3) === "CRT") {
data = data + "'Create':'N',";
}
else if ($(this).attr('id').substring(0, 3) === "RTV") {
data = data + "'Retrive':'N',";
}
else if ($(this).attr('id').substring(0, 3) === "UPD") {
data = data + "'Update':'N',";
}
else if ($(this).attr('id').substring(0, 3) === "DLT") {
data = data + "'Delete':'N'";
}
}
});
if (i == j)
data = data + "}]}";
else
data = data + "}]},";
});
data = data + "]}";