如何……每个循环通过数据检索名称,类型,id下面我的代码看到这有助于我们



如何$。每个循环通过数据检索名称,类型,id下面我的代码看到这有助于我们:

if(this.checked) {
$.ajax(
{
    type: "GET",
    format: 'json',
    url: "dataview.php",
    data: "id=" + fid,
    success: function(data) {
        alert(data);
        $.each(data, function(element, type) {
            alert(element + ": " + type);
            if(type == 'home') {
                $('#home').html('type:home<input type="checkbox">');
            }
        })
        if(type == 'office') {
            $('#office').html('type:office<input type="checkbox">');
        }
    }
});
}

你没有告诉我们data是什么样子的,但我怀疑它是这样的:

{"name":"some name", "type": "some type", "id": "some id"}

如果是这种情况,那么您只需调用data.name, data.typedata.id。此外,还可以通过在代码中使用变量本身来简化if逻辑,如下所示:

$.ajax({
  type: "GET",
  format: 'json',
  url: "dataview.php",
  data: "id=" + fid,
  success: function(data) {
    console.log(data);
    console.log(data.type);
    $('#'+data.type).html('type:'+data.type+'<input type="checkbox">');
  }
});

如果data的结构与我猜测的不同,您需要为我们提供该结构以获得任何进一步的帮助

相关内容

最新更新