RosiReddy
RosiReddy

Reputation: 27

How to make dynamic radio button in dynamic table checked or unchecked in ajax jquery

I wanted to create a dynamic html table after getting response from web method in asp.net using Ajax jquery. I have one field which is boolean value and it is needed to be bind with radio button.But I am not able to set attribute like checked =true or checked = false, and it is allowing only checked = checked attribute. What I need is, if the Boolean field is true. radio button should be checked and remaining should be unchecked. Here is my code

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");
        }
    });
}

Upvotes: 1

Views: 1030

Answers (2)

Musa
Musa

Reputation: 97717

checked is a boolean attribute so once its there the radio button will be checked regardless of its value. Just leave it out to have the radio button unchecked.

Upvotes: 0

adeneo
adeneo

Reputation: 318302

A radio button is checked as long the checked attribute is present, it doesn't matter if it's true or false, any of these will set the button as checked

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

the bottom one is the correct usage.

This means you have to do

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

Upvotes: 1

Related Questions