在概念上,我在思考如何实现这个(绑定到哪些事件等)。
我正在使用CakePHP并查看以下内容:
我的简单测试工作得很好--使用内联-php,我在两个脚本标记之间的页面上放置了一些JS。
<script>
var value = accounting.unformat(<? echo $product['Product']['price'] ?>); // clean up number (eg. user input)
var target = "<? echo SessionComponent::read('User.Preference.currency'); ?>"; // or some user input
var convertedValue = fx(value).from("<? echo $product['Currency']['currency'] >").to(target);
accounting.formatMoney(convertedValue, {
symbol: target,
format: "%v %s"
}); // eg. "53,180.08 GBP"
alert(convertedValue);
</script>很好。效果很好。但我想不出的是如何在有N个产品的页面上实现这一点。
我假设我创建了一个JS函数,类似于:
fx_convert(divid, price, fromcurrency, tocurrency)在我的Cake视图中,我使用内联php来回显函数参数。
对于这个函数,使用jQuery的干净方法是什么,让价格‘div’调用fx_convert并用转换后的值更新它们的内容?
还是我对这件事的想法完全颠倒了?我们非常感谢所有的帮助。
发布于 2013-03-31 09:06:11
在睡了一觉之后,想出了办法。:)
使用内联PHP,我通过div属性传递往返货币,并设置一个统一的类名。例如:
<div class="fx" fx-from="<?php echo $product['Currency']['currency']; ?>" fx-to="<?php echo SessionComponent::read('User.Preference.currency') ?>" fx-value="<?php echo $product['Product']['price']; ?>"></div>然后使用下面的jQuery片段遍历"fx“类的所有元素,并执行所需的计算(使用优秀的money.js和accounting.js)
$(document).ready(function() {
$(".fx").each( function( index, element ){
var value = accounting.unformat($(this).attr("fx-value")); // clean up number (eg. user input)
var target = $(this).attr("fx-to"); // or some user input
var convertedValue = fx(value).from($(this).attr("fx-from")).to(target);
$(this).html(accounting.formatMoney(convertedValue, {
symbol: target,
format: "%v %s"
}));
});
});仍然需要重构和整理,但解决方案是存在的。
https://stackoverflow.com/questions/15721322
复制相似问题