2017-07-25 65 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高于'