2017-07-25 43 views
0

我有我的HTML中的麻煩我的JS變量顯示

<td id = "customerName" style="font:12px Arial,Helvetica,sans-serif;color:#000001;padding:3px 0px 3px 10px">Dear&nbsp;<script>document.getElementById("customerName").innerHTML = custName</script>,</td> </tr> <tr>

我創建了我工作的公司的電子郵件模板。我似乎無法讓JS變量custName顯示在HTML片段ID'd customerName中,我已經在上面發佈了一個表。

編輯:

這裏是我完整的HTML代碼,它是用SuiteScript API

/** 
 
* @NApiVersion 2.x 
 
* @NScriptType UserEventScript 
 
* @NModuleScope SameAccount 
 
*/ 
 
define(['N/email', 'N/record', 'N/search'],function(email, record, search) { 
 
    function beforeLoad(scriptContext) { 
 
    \t 
 
    } 
 
    function beforeSubmit(scriptContext) { 
 

 
    } 
 
    function afterSubmit(scriptContext) { 
 
    \t var fromId = 450103; // my internal ID 
 
    \t var recordToSend = record.load({ //Fake Customer 
 
    \t \t type: record.Type.CUSTOMER, 
 
    \t \t id : 450104, 
 
    \t \t isDynamic: false 
 
    \t }); 
 
    \t 
 
    \t var salesOrder = record.load({ 
 
    \t \t type: record.Type.SALES_ORDER, 
 
    \t \t id : 536583, 
 
    \t \t isDynamic: false 
 
    \t }); 
 
    \t 
 
    \t var department = salesOrder.getValue({ 
 
    \t \t fieldId : 'department' 
 
    \t }); 
 
    \t 
 
    \t var departmentNames = ['Fire Pits Direct', 'Fireplace Remote Controls', 'Skytech Fireplace Remotes', 'Spotix', 'Kick Ass Grills', 'Teak Furniture Classics']; 
 
    \t var deptName = ""; 
 
    \t 
 
    \t for(var i = 19; i < 25; i++){ 
 
    \t \t if(i == department){ 
 
    \t \t \t deptName = departmentNames[i-19]; 
 
    \t \t } 
 
    \t } 
 
    \t 
 
    \t var subjectForEmail = "Your Order Has Shipped From "+ deptName + "!"; 
 
    \t var custName = "This customer"; 
 
    \t 
 
    \t var emailBody = '<center></center>\ 
 
    \t \t <table style="border:1px black solid;">\ 
 
    \t \t <tbody><tr><td><img src="http://www.spotix.com/images/email/Spotix-family-stores_horizontal.jpg" alt="Spotix Family of stores" align="middle"></td> \t </tr><tr><td bgcolor="#ff880f"><span style="padding-bottom:10px;font-weight: bold;font-family: Arial,Helvetica,sans-serif;font-size:28px;line-height: 34px;">&nbsp;&nbsp;&nbsp;Great News! Your order is on the way...</span></td>\ 
 
    \t \t </tr> <tr>  \t <td id = "customerName" style="font:12px Arial,Helvetica,sans-serif;color:#000001;padding:3px 0px 3px 10px">Dear&nbsp;,</td> </tr> <tr>\ 
 
    \t \t <td style="font:12px Arial,Helvetica,sans-serif;color:#000001;padding:3px 0px 3px 10px">We\'ve shipped your {createdfrom}</td> </tr> <tr> \t \t \t <td align="center" bgcolor="#ff880f" style="font-family:Verdana,Arial,Sans-serif;font-size:16px;color:#000000;font-weight: bold;text-align:left;text-decoration:none!important;padding-top:10px;padding-bottom:10px;padding-left:15px;padding-right:15px">Shipping Method - {shipmethod}</td>\ 
 
    \t \t </tr> <tr>  \t <td width="444" align="left" valign="top" style="font:12px Arial,Helvetica,sans-serif;color:#000001;padding:3px 0px 3px 10px" bgcolor="#EEE9E7"><b>Ship To</b></td> </tr>  <tr>  \t <td style="font-family:Verdana,Arial,Sans-serif;font-\ 
 
    \t \t size:12px;color:#000000;text-decoration:none!important;padding-top:10px;padding-bottom:10px;padding-left:15px;padding-right:15px">{shipaddress}<br></td></tr><tr><td width="444" align="left" valign="top" style="font:12px Arial,Helvetica,sans-serif;color:#000001;padding:3px 0px \ 
 
    \t \t 3px 10px" bgcolor="#EEE9E7"><b>Tracking Info</b></td></tr><tr><td style="font-family:Verdana,Arial,Sans-serif;font-size:12px;color:#000000;text-decoration:none!important;padding-top:10px;padding-bottom:10px;padding-left:15px;padding-right:15px">{shipmethod} \ 
 
    \t \t {trackingnumbers}</td></tr><tr><td width="444" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tbody><tr><td width="444" align="left" valign="top"><table border="0" cellpadding="0" cellspacing="0" height="21" width="100%"><tbody><tr><td \ 
 
    \t \t align="left" bgcolor="#EEE9E7" style="font:12px Arial,Helvetica,sans-serif;color:#000001;padding:3px 0px 3px 10px" valign="bottom"><b>Details</b></td> </tr><tr><td align="left" valign="top" width="262"><span style="font-size: 11px;">QTY:&nbsp;{quantity}</span><span \ 
 
    \t \t style="font-size: 11px;">&nbsp;SKU:</span>{custcol_item_sku}<span class="Apple-tab-span" style="font-size: 11px; white-space: pre;"></span><span style="font-size: 11px;">&nbsp;ITEM:</span>{item.description}</td></tr></tbody></table></td></tr></tbody></table></td></tr>\ 
 
    \t \t </tbody></table>Thanks for your order!Questions? Call us now: Mon-Fri 8-5 CST 888.977.6849<img src="http://www.spotix.com/images/email/Benefits-Member_TP.jpg" alt="Spotix benefits" align="middle">\ 
 
    \t \t <script>document.getElementById("customerName").innerHTML = custName</script>'; 
 
    \t \t 
 
    \t 
 
    \t email.send({ 
 
    \t \t author: fromId, 
 
    \t \t recipients: fromId, 
 
    \t \t subject: subjectForEmail, 
 
    \t \t body: emailBody, 
 
    \t \t isInternalOnly: false 
 
    \t \t 
 
    \t }); 
 
    \t 
 
} 
 

 
    return { 
 
     beforeLoad: beforeLoad, 
 
     beforeSubmit: beforeSubmit, 
 
     afterSubmit: afterSubmit 
 
    }; 
 
    
 
});
var emailBody = '<center></center>\ 
 
    \t \t <table style="border:1px black solid;">\ 
 
    \t \t <tbody><tr><td><img src="http://www.spotix.com/images/email/Spotix-family-stores_horizontal.jpg" alt="Spotix Family of stores" align="middle"></td> \t </tr><tr><td bgcolor="#ff880f"><span style="padding-bottom:10px;font-weight: bold;font-family: Arial,Helvetica,sans-serif;font-size:28px;line-height: 34px;">&nbsp;&nbsp;&nbsp;Great News! Your order is on the way...</span></td>\ 
 
    \t \t </tr> <tr>  \t <td id = "customerName" style="font:12px Arial,Helvetica,sans-serif;color:#000001;padding:3px 0px 3px 10px">Dear&nbsp;,</td> </tr> <tr>\ 
 
    \t \t <td style="font:12px Arial,Helvetica,sans-serif;color:#000001;padding:3px 0px 3px 10px">We\'ve shipped your {createdfrom}</td> </tr> <tr> \t \t \t <td align="center" bgcolor="#ff880f" style="font-family:Verdana,Arial,Sans-serif;font-size:16px;color:#000000;font-weight: bold;text-align:left;text-decoration:none!important;padding-top:10px;padding-bottom:10px;padding-left:15px;padding-right:15px">Shipping Method - {shipmethod}</td>\ 
 
    \t \t </tr> <tr>  \t <td width="444" align="left" valign="top" style="font:12px Arial,Helvetica,sans-serif;color:#000001;padding:3px 0px 3px 10px" bgcolor="#EEE9E7"><b>Ship To</b></td> </tr>  <tr>  \t <td style="font-family:Verdana,Arial,Sans-serif;font-\ 
 
    \t \t size:12px;color:#000000;text-decoration:none!important;padding-top:10px;padding-bottom:10px;padding-left:15px;padding-right:15px">{shipaddress}<br></td></tr><tr><td width="444" align="left" valign="top" style="font:12px Arial,Helvetica,sans-serif;color:#000001;padding:3px 0px \ 
 
    \t \t 3px 10px" bgcolor="#EEE9E7"><b>Tracking Info</b></td></tr><tr><td style="font-family:Verdana,Arial,Sans-serif;font-size:12px;color:#000000;text-decoration:none!important;padding-top:10px;padding-bottom:10px;padding-left:15px;padding-right:15px">{shipmethod} \ 
 
    \t \t {trackingnumbers}</td></tr><tr><td width="444" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tbody><tr><td width="444" align="left" valign="top"><table border="0" cellpadding="0" cellspacing="0" height="21" width="100%"><tbody><tr><td \ 
 
    \t \t align="left" bgcolor="#EEE9E7" style="font:12px Arial,Helvetica,sans-serif;color:#000001;padding:3px 0px 3px 10px" valign="bottom"><b>Details</b></td> </tr><tr><td align="left" valign="top" width="262"><span style="font-size: 11px;">QTY:&nbsp;{quantity}</span><span \ 
 
    \t \t style="font-size: 11px;">&nbsp;SKU:</span>{custcol_item_sku}<span class="Apple-tab-span" style="font-size: 11px; white-space: pre;"></span><span style="font-size: 11px;">&nbsp;ITEM:</span>{item.description}</td></tr></tbody></table></td></tr></tbody></table></td></tr>\ 
 
    \t \t </tbody></table>Thanks for your order!Questions? Call us now: Mon-Fri 8-5 CST 888.977.6849<img src="http://www.spotix.com/images/email/Benefits-Member_TP.jpg" alt="Spotix benefits" align="middle">\ 
 
    \t \t <script>document.getElementById("customerName").innerHTML = custName</script>';

+0

custName從哪裏來?你必須提供更多的代碼。添加一個最簡單的工作/可運行示例,請嘗試 – derHugo

+1

*單擊「運行代碼段」並看到錯誤消息*。那麼定義缺失的變量? – Quentin

+0

變量是'var custName =「This customer」;' –

回答

1

當你寫custName沒有""''一個Javascript程序中的變量, Javascript認爲你指的是一個變量。它尋找一個名稱爲custName的變量,但沒有找到它,所以它會拋出一個ReferenceError

確保custName是一個已定義的變量並且是一個字符串。或者,只需使用普通字符串。

var custName = 'John'; 
document.getElementById("customerName").innerHTML = custName; 

或者只是使用一個字符串。

document.getElementById("customerName").innerHTML = 'John'; 
+0

對不起,我應該把它放在原來的文章。 'var custName =「This customer」;' –

0

我會把腳本標記放在最後以避免阻塞並將custName設置爲字符串。

<td id ='customerName' style='font:12px Arial,Helvetica,sans-serif; 
color:#000001;padding:3px 0px 3px 10px'> 
Dear&nbsp; 
</td> 
<script>document.getElementById("customerName").innerHTML = 'custName'</script> 
+0

但是custName是我JS中的一個變量。這不是僅僅打印字符串'custName'而不是使用變量? –

+0

在這種情況下很酷是'custName'沒有引號就沒問題。只要確保帶有變量的JS高於'

  • 11. 麻煩從我的客戶
  • 12. 麻煩我的Java加密
  • 13. 我遇到有關float.Help麻煩我
  • 14. 複選框中,我有我的GridView的麻煩一個gridview
  • 15. 我會見了MongoDB的麻煩,幫我
  • 16. 的Android tablelayout麻煩,加上我有麻煩意見
  • 17. 有麻煩我的DataGridView排序
  • 18. 我有麻煩的onResume方法
  • 19. 有麻煩移動我的skspritenodes編輯:
  • 20. 我有一些麻煩的分隔符
  • 21. 有麻煩我的模型建立
  • 22. 我的jQuery代碼有些麻煩
  • 23. 我有一些PHP代碼的麻煩
  • 24. 的JavaScript/HTML可變麻煩
  • 25. 我有麻煩的Python功能,有人可以幫助我嗎?
  • 26. 我有一個麻煩與我的登錄認證的Servlet
  • 27. 你怎麼算的我有我的程序麻煩菜單
  • 28. 我的AJAX POST請求到我的PHP文件有點麻煩
  • 29. 我有麻煩的file_get_contents()到我的服務器
  • 30. 麻煩與我遇到的CSS父/子麻煩的CSS父/子