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 调用,也许您可​​以在服务器端处理它?但你可能有你的理由。

首先你需要使用$(document).ready();event,你不能开始将东西附加到一个可能还没有在你的 DOM 中的标签上。

你的if 语句是无用 if (responce.jsonWrapperforGrid.rows.length > 0)的,如果长度为 0,你的 .each() 循环将不会做任何事情,之前不需要测试。

更糟糕的是,您开始在 .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 回答