迭代Ajax响应并在某些条件下创建动态表

Iterating Ajax response and creating Dynamic table on some condition

本文关键字:条件下 创建 动态 Ajax 响应 迭代      更新时间:2023-09-26

我试图创建一个ajax响应动态表。然后在复选框上加上条件,比如if item。statuscheck =1将打印带有名称和项目的复选框。Statuscheck =0复选框将以相同的名称显示。我可以动态地显示name。但是问题出现在复选框上。这是我正在尝试的代码。请帮我解决这个问题。

  success(response)
{
    $.each(response,function(index,item){
        var status;
        if(index >=0 and index <= 4)
    {
        if(item.statusCheck = 1)
        {
            status = <input type ="checked" checked/>
        }
        else{
            status =  <input type="checked" />
        }   
        var content = '<td>'+ status + '</td>'
                 +<td>' + item.fieldName +'<td>';
            $("#tableId").appent(content)
    }
  }
}

("# tableId").append(内容)

更改appent为

比较操作符是==而不是=的赋值。还要注意'"

 success(response)
{
    $.each(response,function(index,item){
        var status;
        if(0 >= index <= 4)
    {
        if(item.statusCheck == 1)
        {
            status = '<input type ="checkbox" checked/>';
        }
        else{
            status =  '<input type="checkbox" />';
        }   
        var content = '<td>'+ status + '</td>'
                 +'<td>' + item.fieldName +'<td>';
            $("#tableId").append(content);
    }
  }
}
var content = item.statusCheck == 1 ? '<td><input type="checkbox" checked="checked" />'+item.fieldName+'</td>' : '<td><input type="checkbox" />'+item.fieldName+'</td>';
$("#tableId").append($(content));

不是type = "checked",是type = "checkbox"

success(response)
{
    $.each(response,function(index,item){
        var status;
        if(index >=0 and index <= 4)
    {
        if(item.statusCheck = 1)
        {
            status = <input type ="checkbox" checked/>
        }
        else{
            status =  <input type="checkbox"/>
        }   
        var content = '<td>'+ status + '</td>'
                 +<td>' + item.fieldName +'<td>';
            $("#tableId").appent(content)
    }
  }
}