当我输入一个£符号时,用jQuery输出html会创建一个随机的“”

时间:2017-01-24 14:09:48

标签: javascript jquery

当我从下拉列表中选择一个新选项时,我有以下函数用于填充一些带有一些文本的跨度。

     jQuery('#finance_option').on('change', function() {
        var totalPrice = jQuery('.summary .price ins .woocommerce-Price-amount.amount').text();
        var number = Number(totalPrice.replace(/[^0-9\.]+/g,"")); 
        number = parseFloat(number);
        var productGuid = this.value;

        arr = lookup_table(productGuid);

        var apr = parseFloat(arr.apr);
        var duration = parseFloat(arr.duration);
        var calculation = parseFloat(arr.calculation);

        var deposit = round((number / 100) * 10);
        var monthly = round((number - deposit) * calculation);
        var final = monthly;
        var loan_amount = round(number - deposit);
        var loan_repayment =round(monthly * (duration -1) + final);
        var cost = round(loan_repayment - (number - deposit));
        var total = round(loan_repayment + deposit);

        jQuery('#deposit').html('10%');
        jQuery('#monthly').html('£' + monthly);
        jQuery('#final').html('£' + monthly);
        jQuery('#cash').html('£' + number);
        jQuery('#deposit_to_pay').html('£' + deposit);
        jQuery('#loan_amount').html('£' + loan_amount);
        jQuery('#loan_repayment').html('£' + loan_repayment);
        jQuery('#cost').html('£' + cost);
        jQuery('#total').html('£' + total);
        jQuery('#number').html(duration);

    });
});

大部分时间都可以正常工作,并将输出正确显示到html表中,如下所示。

Deposit               |     10%
Monthly Payment       |     £81.11
Final Installation    |     £81.11
Cash price            |     £1800   
Deposit to Pay        |     £180
Loan Amount           |     £1620
Loan Repayment        |     £1946.64
Cost of Loan          |     £326.64
Total Amount Payable  |     £2126.64
Number of Payments    |     24

然而,有时当我重新加载或选择新选项时,输出看起来像这样。

Deposit               |     10%
Monthly Payment       |     £81.11
Final Installation    |     £81.11
Cash price            |     £1800   
Deposit to Pay        |     £180
Loan Amount           |     £1620
Loan Repayment        |     £1946.64
Cost of Loan          |     £326.64
Total Amount Payable  |     £2126.64
Number of Payments    |     24

输出的html对于每一行都是这样的

  <td id="loan_repayment">£1946.64</td>

这是什么'',它来自哪里?如何阻止它?

2 个答案:

答案 0 :(得分:3)

尝试用

逃脱英镑符号
&#163;

e.g:

jQuery('#total').html('&#163;' + total);

答案 1 :(得分:2)

问题在于编码。很可能你已经用UTF-8保存了文件而没有这样做,所以函数会写出两个字符。

幸运的巧合是,有代码点U + 00A3编码为UTF-8是0xC2 0xA3所以它看起来像是一个额外的字符被添加到井号并且不是完全垃圾。

请检查保存的文件的编码,服务器给出的编码,页面的编码等。它应该都匹配正确。