2015-06-24 35 views
2

好吧,所以我有一個服務器端數據表讀取JSON格式的子行。 If/Else If/Switch之間的語句如何?添加如果/否則如果在Javascript內部陳述HTML

<script type="text/javascript"> 

function format (d) { 
return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">', 

    '<tbody>'+ 
    '<tr>'+ 
     '<td class="second-td-box">'+ 
      '<p class="at-a-glance">Basic information:</p>'+ 
      '<p><span class="subhead">Name: </span>'+d.RES_GUEST_FIRSTNAME+'</p>'+ 
     '</td>'+ 
    '</tr>'+ 
    '</tbody>'+ 
'</table>'+ 
</script> 

我曾嘗試:

<script type="text/javascript"> 

function format (d) { 
return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">', 

    '<tbody>'+ 
    '<tr>'+ 
     '<td class="second-td-box">'+ '' 
      if (d.RES_GUEST_FIRSTNAME == "Alex") { 
      '<p class="at-a-glance">Basic information:</p>'+ 
      '<p><span class="subhead">Name: </span>'+d.RES_GUEST_FIRSTNAME+'</p>'+ 
      } 
     '</td>'+ 
    '</tr>'+ 
    '</tbody>'+ 
'</table>'+ 
</script> 

使用下面的代碼解決了問題:

((d.BOOKING_SOURCE_ID == 73844)? 
     '<td class="second-td-box">'+ 
      '<p class="at-a-glance">Airbnb information:</p>'+ 
      '<p><span class="subhead">Confirmation: </span>'+d.CONFIRMATION+'</p>'+ 
      '<p><span class="subhead">Agency Income: </span>'+d.AIRBNB_AGENCY_INCOME+'</p>'+ 
      '<p><span class="subhead">Airbnb Income: </span>'+d.AIRBNB_INCOME+'</p>'+ 
      '<p><span class="subhead">Airbnb fees: </span>'+d.AIRBNB_FEES+'</p>'+ 
      '<p><span class="subhead">Airbnb per night: </span>'+d.AIRBNB_PER_NIGHT+'</p>'+ 
      '<span id="chart_div"></span>' 
      :"")+ 
     '</td>'+ 


    ((d.BOOKING_SOURCE_ID == 73858)? 
      '<td class="second-td-box">'+ 
      '<p class="at-a-glance">Booking.com information:</p>'+ 
      '<p><span class="subhead">Booking date: </span>'+d.BOOKING_DATE+'</p>'+ 
      '<p><span class="subhead">Booking currency: </span>'+d.BOOKING_CURRENCY+'</p>'+ 
      '<p><span class="subhead">Booking total: </span>'+d.BOOKING_TOTAL+'</p>'+ 
      '<p><span class="subhead">Booking comission: </span>'+d.BOOKING_COMISSION+'</p>'+ 
      '<p><span class="subhead">Booking policy: </span>'+d.BOOKING_POLICY+'</p>'+ 
      '<span id="chart_div"></span>' 
      :"")+ 
     '</td>'+ 
     '</tr>'+ 

看來我只能 「打印」 使用條件運算符。

回答

1

您可以嘗試條件運算符(https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Operators/Conditional_Operator):

<script type="text/javascript"> 

function format (d) { 
return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">', 

    '<tbody>'+ 
    '<tr>'+ 
     '<td class="second-td-box">'+ '' + 
      ((d.RES_GUEST_FIRSTNAME == "Alex")? 
      '<p class="at-a-glance">Basic information:</p>'+ 
      '<p><span class="subhead">Name:</span>'+d.RES_GUEST_FIRSTNAME+'</p>' 
      :"")+ 
     '</td>'+ 
    '</tr>'+ 
    '</tbody>'+ 
'</table>'+ 
</script> 
+0

解決您的幫助。謝謝! –

+0

如果你想要做/ if/else,你可以像這樣嵌套條件運算符: (a =='1'?'text for 1':(a =='2'?'Text for 2' : '')) – Gunni

0

如果與串聯while語句不能使用。您可以使用conditional operator是這樣的:

'string' + d.RES_GUEST_FIRSTNAME ? 'add string' : '' + 'another string'; 
0

基本上你正在返回一個字符串,這樣你就可以建立位這件事位。我會強調,從JavaScript返回HTML作爲字符串不是推薦的注入HTML方式。

var returnString = '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'; 

returnString += '<tbody>'; 
returnString += '<tr>'; 

等..

,如果你想在一個如果/當別人

,那麼你可以這樣做

添加然後返回字符串

return returnString; 

所以

if (d.RES_GUEST_FIRSTNAME == "Alex") { 
    returnString += '<p class="at-a-glance">Basic information:</p>'; 
}