来自AJAX调用的jQuery更改表值

时间:2018-09-02 19:26:21

标签: jquery ajax

嗨,我正在使用来自响应JSON的ajax构建表。因此,如果在我的响应数据中有一个价格对象。如果价格为空,我想将其显示为0。

这是我的AJAX代码-

    $(document).ready(function(){

var url ="data/response.json";
var data=""; 
coAjax(url, data, function(result){
  var availableProductsList = result.response.availableProducts;
  var terminalProductsList = result.response.availableProducts;
  console.log(availableProductsList);
  var output="<tbody>";

        for (var i in availableProductsList){ 
           output += "<tr><td>" 
                    + result.response.availableProducts[i].productId + 
                    "</td><td>" 
                    + result.response.availableProducts[i].productName + 
                    "</td><td>" 
                    + result.response.availableProducts[i].quantity + 
                    "</td><td>" 
                    + "<div class='replace-split-key-tbl-inputbox'>" + "<input type='text' class='form-control key-input key-input-tbl' id="+ result.response.availableProducts[i].productId +">" + "</div>" + 
                    "</td><td><span class='availableFeaturesPriceCol'>"+ result.response.availableProducts[i].price +"</span></td></tr>";

        console.log(result.response.availableProducts[i].price);

        }

        output+="</tbody>";
 $("#addfeaturesOrderTbl").append(output);
 $("table").addClass("table");
});
});

function coAjax(url, data, handleData) {
    $.ajax({
    url:url,
    dataType: 'json',
    contentType: 'application/json',
    data:JSON.stringify(data),
    success:function(result) {
        handleData(result); 
    }
  });
}

对于价格列,如果价格为空,我想将其显示为0。这是获取价格result.response.availableProducts[i].price的代码。请帮忙。

3 个答案:

答案 0 :(得分:1)

您可以在其后添加一个Logical OR (||) operator,如果它是虚假的,它将使用零代替

class tri:
def __init__(self,x=0):
    self.x=x
def __str__(self):
    return '({0})'.format(self.x)
def __add__(self,one,two):
    length=self.x+one.x+two.x
    return length


>>>perp=tri(5)
>>>print(perp)
>>>base=tri(8)
>>>print(base)
>>>hyp=tri(9)
>>>print(hyp)
>>>print (perp+base+hyp)

答案 1 :(得分:0)

var price = result.response.availableProducts[i].price;
price != null ? output+=`<td>${price}</td>` : output+="<td>0</td>";

答案 2 :(得分:0)

transform.normalize