4

它在 textarea 中显示€而不是 €(货币符号)。有谁知道为什么会出错?

<?php
$currency = "&euro;"; //using php with other data from database

echo "<script>
      $('#share_button').click(function(e){
      // pass the currency to javascript and put in textarea shared with other on clicks
           $('#snsdescp').val(`".$currency."`);
      });</script>";
?>

 //shared textarea
<textarea class="form-control" name="message" id="snsdescp"></textarea> 
4

4 回答 4

3

val()方法不进行编码/解码,作为一种技巧,您可以使用该html()函数进行编码,然后剥离文本:

$('#share_button').click(function(e){
    $('#snsdescp').val($("<div>").html("&euro;").text());
});

这是一个适用于您的 textarea 的工作 jsFiddle

于 2016-11-24T05:17:03.093 回答
0

您可以通过分配&euro;给元素的innerHTML属性来设置它:textarea

document.querySelector('#snsdescp').innerHTML = '&euro;';

如果你想使用 jQuery,你可以简单地调用它的.html()方法:

$('#snsdescp').html('&euro;');
于 2016-11-24T05:19:47.653 回答
0

好吧,我不明白为什么要存储在 php 变量中,但可能您可以存储在 javascript 变量中。它可以帮助你。

<?php
  $currency = "\u20ac"; //using php with other data from database
?>

 <textarea class="form-control" name="message" id="snsdescp"></textarea> 
  <br/><br/>
  <div id="share_button" >hello</div>//suppose your id is here

 <script>
    var value ="<?php echo $currency;?>"; //store php value in js variable
    $("#share_button").click(function(){

        $("#snsdescp").val(value);

     });
  </script>
于 2016-11-24T06:00:21.780 回答
0

您可以将 Unicode 用于 € \u20AC

$('#snsdescp').val("\u20AC");

于 2016-11-24T06:06:44.553 回答