Click here to Skip to main content
14,695,321 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Creating table using jquery. In one of the column(UomIsBaseUnit) in my table has checkbox. I want to check the checkbox depending upon the datas. For example if the data is 1 means, It should be checked otherwise not.

What I have tried:

<pre>  $(document).ready(function () {
            var baseUrl = '@Url.Content("~/")';
            var apiurl = baseUrl + "api/Uom/Get";
            $.ajax({
                url: apiurl,
                type: "Get",
                success: function (data) {
                    $.each(data,
                        function (key, item) {
                            var chk = item.UomIsBaseUnit = 1 ? true : false;
                            $('#tbluom').append("<tr><td>" +
                                item.UomName +
                                "</td>" +
                                "<td>" +
                                item.UomDescription +"</td><td>  <input 
                               type='checkbox' checked="+ chk +">"+
                                "</td><td> <a onclick='GetDetails(" + item.UomId + 
                               ")'>Edit</a>" +
                                "<a onclick='DeleteDetails(" + item.UomId + 
                               ")'>Delete</a></td></tr>"
                            );
                        });
                },
                error: function (msg) { }
            });


It is not working
Posted
Updated 27-Nov-17 2:36am
v3

use like below it will work
var chk = item.UomIsBaseUnit == 1 ? "checked='checked'" : "";
<input type='checkbox' + chk />
   
v2
Comments
Member 13542747 30-Nov-17 1:36am
   
I tried this one
if(item.UomIsBaseUnit == 1)
{
chk=true;
}
<input type='checkbox' + chk />
Member 13542747 30-Nov-17 1:36am
   
This code is working for me
Ok, based on what presented, I assumed the chk always return false even though item.UomIsBaseUnit = 1 ? In JavaScript, the = is an assignment operator, on the other hand == is an Equality Comparison. The original code is assigning 1 to the item.UomIsBaseUnit object. It should check if item.UomIsBaseUnit equal 1 using "==".

var chk = item.UomIsBaseUnit == 1 ? true : false;
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900