到目前为止,我有这个由片段组成。到目前为止,我还不是 JavaScript 方面的专家,所以如果有人能帮助我实现以下目标,我将不胜感激,并希望今天能学到一些新东西 :)
我想实现以下目标:
当用户在输入字段中输入 1000000 时,显示的结果如下所示,
- 超过 100 万美元
- 低于 100 万美元
- 在 97 万美元到 130 万美元之间
目前我可以实现价格数字的正确显示,但不知道如何在价格末尾添加百万、千、百。另外,我不知道如何减去 3% 并将中间价格部分的价格增加 3%。
到目前为止,这是我的代码:
<input type="text" id="price" class="liveprice" value="<?php echo $myprice; ?>" >
<p>Higher than <span id="higher"><?php echo $myprice;?></span></p>
<p>Lower than <span id="lower"><?php echo $myprice;?></span></p>
<p>In between <span id="between"><?php echo $myprice;?></span></p>
<script type="text/javascript">
// make sure it adds commas and dots to price
$.fn.digits = function() {
return this.each(function() {
$(this).text($(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
})
}
// update in real time
$("input.liveprice").keyup(function() {
var value = $(this).val();
$("#higher").text(value).digits();
$("#lower").text(value).digits();
$("#between").text(value).digits();
}).keyup();
</script>