-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

after alot of googling I did it below way, may be it will help some one

                   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 に答える