【问题标题】:How to make dynamic radio button in dynamic table checked or unchecked in ajax jquery如何在ajax jquery中选中或取消选中动态表中的动态单选按钮
【发布时间】:2015-02-23 05:59:36
【问题描述】:

我想在使用 Ajax jquery 从 asp.net 中的 web 方法获得响应后创建一个动态 html 表。我有一个布尔值字段,它需要与单选按钮绑定。但我无法设置属性,如checked = true 或checked = false,它只允许checked = checked 属性。我需要的是,如果布尔字段为真。单选按钮应选中,其余应取消选中。这是我的代码

function GetDetails(StId) {
    var input = '{StId:' + StId + '}';
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "<%=Page.ResolveUrl("~ / Sample.aspx / Getdetails ")%>",
        data: input,
        dataType: "json",
        success: function (Result) {
            Result = Result.d;
            $("#tblImages").empty();
            $.each(Result, function (Key, Value) {
                var id = Value.Id;
                var url = "Images.ashx?ImgID=" + id;
                if (Value.desc == null) var desc = '';
                if (Value.IsMainImg == true);
                mImg = 'checked';
                $("#tblImages").append("<tr><td><img  src='" + url + "' style='border:3px solid #CCCCFF;height:78px;width:118px'/></td></td><td><table class='subImg' style='border-collapse: separate; border-spacing: 10px;'><tr><td colspan='2'><input  type='text' style='height:25px; width:250px' value='" + desc + "' /></td></tr><tr><td style='width:55px'><input id='" + id + "' class='btnDelete' type='submit' value='Delete'/></td><td><input class='rdSetAsMainImg' id='" + id + "' type='radio' checked=' " + mImg + "'/>Set as Main Image</td</tr></table></td></tr>");
            });
        },
        error: function (Result) {
            alert("Error");
        }
    });
}

【问题讨论】:

  • 你的 json 看起来有点古怪,asp.net 接受无效的 json 吗?
  • json没有问题。我的问题解决了。感谢您的回复

标签: jquery html asp.net ajax asp.net-mvc


【解决方案1】:

只要存在checked 属性,就会检查单选按钮,无论它是真还是假,其中任何一个都会将按钮设置为checked

checked="true"
checked="false"
checked="checked"

下面是正确的用法。

这意味着你必须这样做

"<input id='"+id+"' type='radio'" + (mImg ? ' checked="checked"' : '') + " />"

【讨论】:

  • 非常感谢您的解决方案
【解决方案2】:

checked 是一个布尔属性,所以一旦它存在,无论其值如何,都会检查单选按钮。只需保留它即可取消选中单选按钮。

【讨论】:

    猜你喜欢
    • 2014-05-11
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2016-12-23
    • 2020-02-12
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多