如何将图像添加到我的javascript If语句中?

时间:2014-04-30 16:18:29

标签: javascript jquery image

for(var i = 0; i < data.invoices.length; i++) {

  var unix_timestamp = data.invoices[i].invoiceDate;

var date = new Date(unix_timestamp);
// hours part from the timestamp
var day = date.getDate();
// minutes part from the timestamp
var mnth = (date.getMonth())+1;
// seconds part from the timestamp
var yr = date.getFullYear();

var p = "";

if(data.invoices[i].paid == false) {
p = "Outstanding";
} else {
  p = "Paid";
}


if(i%2 == 1) {
  tableRow = '<tr>';
} else {
tableRow = '<tr class="EDBGreyBackground">';
}

tableRow += '<td height="40" valign="middle" align="left">'+day+'-'+mnth+'-'+yr+'</td>';
tableRow += '<td height="40" valign="middle" align="left">&pound;'+data.invoices[i].amountDue+'</td>';
tableRow += '<td height="40" valign="middle" align="left">'+p+'</td>';
tableRow += '<td height="40" valign="middle" align="left"><a href="'+data.invoices[i].printableBill+'"><img class="floatNone" width="25" height="25" border="0" alt="PDF" src="images/EDB_PDF_icon.png"></a></td>';
tableRow += '</tr>';

$('#previousBillhistory').append(tableRow);

  }

我正在尝试将我的IF语句中的PAID更改为图像,任何人都可以指出我正确的方向。

谢谢

更新了整个代码段。

再次感谢您

1 个答案:

答案 0 :(得分:0)

您可以使用div并根据付费值更改其内容:

<div id="yourImageDiv"></div>

然后在你的代码中尝试类似:

if(data.invoices[i].paid == false){
    p = "Outstanding";
} else {
    $('#yourImageDiv').empty();
    $('#yourImageDiv').html('<img id="img" src="img.png" />')
}

更新问题后 ....您需要的更简单。只需这样做:

if(data.invoices[i].paid == false) {
    p = "<img src='Outstanding_image.png' />"; 
} else {
    p = "<img src='Paid_image.png' />";
}