-1

我想基于 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 字符串吗?或任何其他方式..请帮助我

4

1 回答 1

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 + "]}";
于 2012-05-20T06:41:32.823 回答