javascript - 如何使用jquery获取gridview中选定单选按钮的值

标签 javascript jquery html ajax

我正在开发 wijgrid,并且我已经为 html 表单提供了一个单选按钮。 我已经使用 jquery 来获取单选按钮的值,它显示在表单上,​​但没有显示在网格上。

我想在选择单选按钮时获取参数代码的值,并且该值应该显示在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>

最佳答案

这真的都是你的代码吗? 如果您开始在加载时进行 ajax 调用,也许您可​​以在服务器端处理这些事情?但你可能有你的理由。

首先您需要使用 $(document).ready();事件,您无法开始向可能不在 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
            })
        }
    });
});

关于javascript - 如何使用jquery获取gridview中选定单选按钮的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11947186/

相关文章:

javascript - 在指定时间遍历 JSON 对象(例如电影字幕)

javascript - Javascript 中的迷宫生成器未捕获类型错误

jquery 使用变量分配和调用 ID

javascript - 为什么即使我已经包含了引用资料,jquery marquee 也不起作用?

jquery - 如何自定义复选框,添加背景图片

javascript - 按引用复制

javascript - 使用 jquery 显示输入文件上传大小限制的客户端验证消息?

java - 在 Java 中读取和修改 HTML 文件

html - CSS float 溢出

javascript - 配置 Typescript 项目以使用 Bable 从 ES6 转换为 ES5