1

私はwijgridに取り組んでおり、htmlフォームにラジオボタンを付けました。jqueryを使用してラジオボタンの値を取得しましたが、フォームには表示されていますが、グリッドには表示されていません。

ラジオ ボタンの選択時に paramcode の値を取得したいのですが、この値は wijgrid に表示されるはずです。 .

助けてください...ありがとうティナ!!

これは私のJSONです(読みやすくするために再フォーマットされていますが、実際には縮小されています):

{
    "jsonWrapperforGrid": {
        "page": "1",
        "total": "2",
        "rows": [
            {
                "tenantId": 0,
                "paramType": "UserGender",
                "paramCode":"F", 
                "langCode":"en", 
                "paramValue":"Female"
            },
            {
                "tenantId": 0,
                "paramType": "UserGender",
                "paramCode": "M",
                "langCode": "en",
                "paramValue": "Male", 
                "paramBlob": ""
            }
        ]
    }
}

これは私のjQueryスクリプトです:

<script type="text/javascript">
$.ajax({ 
    url: "UserGender",
    type: "GET",
    dataType: "json",
    contentType : "application/json",
    success: function (responce) { 
        if (responce.jsonWrapperforGrid.rows.length > 0) {
            $.each(responce.jsonWrapperforGrid.rows, function (i, entity) {  
                $('#gencom01').append(
                    $('<label />', { 'for': 'gender' + entity.paramValue,
                                     'text': entity.paramValue }),
                    $('<input />', { 'id': 'gender' + entity.paramCode,
                                     'type': 'radio', 
                                     'name': 'gender', 
                                     'value': entity.paramValue })
                    .click(function() {
                        var inputValue = $('input:radio:[name=+"gendernew"]:checked').val(entity.paramCode);
                        $("#gencom").val(entity.paramCode );
                    })
                );
            });
        }
    }
});
</script>

これは私のHTMLです:

<body>  
  <div id="gencom01">
    <td><input id="gencom" style="width:205px ;" name="gendernew">
  </div>
</body>
4

1 に答える 1

1

それは本当にすべてのコードですか?ロード時に ajax 呼び出しを開始した場合、サーバー側で処理できた可能性があるのではないでしょうか? しかし、おそらくあなたにはあなたの理由があります。

最初にeventを使用する$(document).ready();必要があります。おそらくまだ DOM にないタグに何かを追加することはできません。

if ステートメントは役に立たない .each if (responce.jsonWrapperforGrid.rows.length > 0)() ループは、長さが 0 の場合は何も実行しないため、事前にテストする必要はありません。

さらに悪いことに、.append() 内で .click() を宣言し始めました。これは機能する可能性がありますが、少し奇妙に見え、多くのエラーの原因になる可能性があります。通常、DOM 操作とイベントを分離しておくと、デバッグが容易になります。そして、より最新の .on() を使用してください。

AJAX 呼び出しの背後にあるテクノロジはわかりませんが、JSONを実際の JS オブジェクトに解析すると役立ちます。var jsonData = $.parseJSON(responce);

したがって、できるだけ少ない .append() を作成することをお勧めします。ループで使用すると時間がかかる場合があります。データを変数に保存することをお勧めします。ループの最後でのみ、すべてを追加できます。

<td>そして、これがあなたの中で何をしているのかわかりません<div>

コードは次のようになります。JSON がどのように見えるかがわからないため、何もテストできませんでした。

$(document).ready(function()
{
    $.ajax({ 
        url: "/mgw/pankanis/admin/SysParameter?Paramtype=UserGender",
        type:"GET",
        dataType: "json",

        contentType : "application/json",
        success: function (responce)
        {
            var jsonData = $.parseJSON(responce);
            var toAppend = "";
            $.each(jsonData.jsonWrapperforGrid.rows,function(i,entity)
            {
                toAppend += '<label for="gender'+entity.paramValue+'">'+entity.paramValue+'</label>';
                toAppend += '<input id="gender'+entity.paramCode+'" type="radio" name="gender" value="'+entity.paramValue+'">';
            });
            $('#gencom01').append(toAppend);
            $("#gencom01 input:not(#gencom)").on("click",function()
            {
                $("#gencom").val($(this).attr("id").substr(6)); // entity.paramCode
            })
        }
    });
});
于 2012-08-14T07:44:48.970 に答える