简体   繁体   English

如何将javascript函数中的值使用到smarty模板中?

[英]How to use value from javascript function into smarty template?

I want to use the value of textarea present within a template. 我想使用模板中存在的textarea的值。 My code is as below: 我的代码如下:

<tr>
  <td style="background-color:#ffffff; padding:40px 22px; font-family:Arial, Helvetica, sans-serif; color:#505050; font-size:12px; font-weight:normal;">
    **[[NEWSLETTER_BODY]]**
  </td>
</tr>

I want to use the value from the Javascript function below at the place [[NEWSLETTER_BODY]] . 我想在[[NEWSLETTER_BODY]]处使用下面Javascript函数的值。 Can you help me in this regard? 您能在这方面帮助我吗? Thanks in advance. 提前致谢。

{literal}
<script language="javascript" type="text/javascript">
function get_text_area_value(){ 
  var email_body = jQuery("textarea#newsletter_email_body").val();
  //alert(email_body);
}
</script>
{/literal}

Actually I want to call this function in following fashion. 实际上,我想以以下方式调用此函数。 At the same time the value should be there in the smarty template. 同时,值应该在smarty模板中。

<input type="button" name="btn_preview" id="btn_preview" value="{$preview_value}" class="submit" id="preview_newsletter" onclick="get_text_area_value()">

The contents of the newsletter will have to be submitted to the server in order for them to be available to Smarty. 新闻通讯的内容必须提交到服务器,以便Smarty可以使用。 Since you'll need to submit anyway, you won't need to do anything special - just submit the form using the submit button you already have. 由于仍然需要提交,因此您不需要做任何特别的事情-只需使用已有的提交按钮提交表单即可。

If you don't want to submit the form, but prefer to show the contents of the textarea somewhere else on the page and properly formatted, then you won't need Smarty. 如果您不想提交表单,而是希望在页面上的其他位置显示文本区域的内容,并且格式正确,那么您就不需要Smarty。 Instead, use JavaScript to grab the textarea contents (you already have that code) and append the contents to a element somewhere else, eg 取而代之的是,使用JavaScript来获取文本区域的内容(您已经有了该代码),然后将内容附加到其他地方的元素中,例如

var email_body = jQuery("textarea#newsletter_email_body").val();
$("mydiv").innerHTML = email_body;

You may need to sanitize the HTML contents of the textarea first. 您可能需要先清理文本区域的HTML内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM