简体   繁体   English

javascript用点/贝宝替换逗号

[英]javascript replace comma with dot / paypal

I have a Paypal button: Paypal allows only a DOT in the field: amount, tax, shipping 我有一个Paypal按钮:Paypal在该字段中仅允许DOT:金额,税金,运费

<form target="paypal" name="paypal" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" id="paypalbutton">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="email.test@paypal.de">
<input type="hidden" name="item_name" value="testname">
<input type="hidden" name="item_number" value="order234">
<input type="hidden" name="amount" value="2,25">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="EUR">
<input type="hidden" name="lc" value="DE">
<input type="hidden" name="tax" value="1,10">
<input type="hidden" name="shipping" value="10,20">
<input type="hidden" name="cancel_return" value="www.test.com" >
<input type="hidden" name="return" value="www.test.com" >
<input type="submit" name="submit" value="Bezahlung mit Kreditkarte via PayPal" alt="Bezahlen mit Kreditkarte via PayPal" class="btn btn-default">
</form>

only the three inputs need a DOT in the price! 价格中只有三个输入需要DOT! paypal allow only a DOT, not a COMMA 贝宝只允许DOT,而不允许COMMA

name="amount"
name="tax"
name="shipping"

here is a js for one input, but I need this for the three input:/ 这是一个用于输入的js,但是对于三个输入,我需要这个:

<script type="text/javascript">
var point = document.forms[0].amount.value;
var comma = point.replace(/,/, ".");
document.forms[0].amount.value = comma;
</script>

can you not do the same for the others? 你不能为别人做同样的事情吗? put that code into a function and send the name of the field through as an argument. 将该代码放入函数中,然后将字段名称作为参数发送。

function change_comma(input_name){
  var point = document.forms[0][input_name].value,
      comma = point.replace(/,/, ".");
  document.forms[0][input_name].value = comma;
}

change_comma('amount')
change_comma('tax')
change_comma('shipping')

note that I am referencing the variable in the function using square brackets. 请注意,我使用方括号引用了函数中的变量。 This allows you to reference the portion of the object contained within the variable. 这使您可以引用变量中包含的对象部分。

Rather than relying on JavaScript to transform the , to a . 而不是依赖于JavaScript进行改造,以一个. , do it on the server side. ,在服务器端执行此操作。 After all, some clients might have JavaScript disabled. 毕竟,某些客户端可能已禁用JavaScript。

The fields are all hidden input fields, so presumably generated by server-side code. 这些字段都是hidden输入字段,因此大概是由服务器端代码生成的。

Further, I would suggest never storing actual payment amounts in the HTML code. 此外,我建议不要在HTML代码中存储实际的付款金额。 Doing so makes it possible for someone to use a browser plugin (or simply the F12 tools on most browsers) to change the amount they are about to pay. 这样做使某人可以使用浏览器插件(或大多数浏览器中的F12工具)来更改他们将要支付的金额。

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

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