如何在将Json返回表时使用if-else条件使用trHtML + =

时间:2019-04-12 15:10:52

标签: php ajax laravel jquery-mobile

$.ajax({
    type: "POST",
    dataType: "json",
    url: "/section/search_go",
    data: { query: search_string },
    cache: false,
    success: function(response) {
        console.log("Got " + response.length + " results");
        if (response.length === 0) {
            $("#customers").html(no_results_str);
        } else {
            var trHTML = "";
            $.each(response, function(i, item) {
                trHTML += "<tr>";
                trHTML += "<td>" + item.accountnumber + "</td>";
                trHTML += "<td>" + item.company + "</td>";
                trHTML += "<td>" + item.phone + "</td>";
                trHTML += "<td>" + item.postcode + "</td>";

                /// want to use
                      if (item.status = 1) {
                          echo "Complete";
                      }else{
                          echo "Non Complete";
                      }
                */////////////////////////////////

                trHTML += "</tr>";
            });
            $("#customers").html(trHTML);
        }
    }
});

1 个答案:

答案 0 :(得分:0)

您正在使用变量响应,但是可以将其定义为length属性。

screenshot

尝试

if(! response typeof undefined)

代替

if(response.length === 0)