3

到目前为止,我有这个由片段组成。到目前为止,我还不是 JavaScript 方面的专家,所以如果有人能帮助我实现以下目标,我将不胜感激,并希望今天能学到一些新东西 :)

我想实现以下目标:

当用户在输入字段中输入 1000000 时,显示的结果如下所示,

  1. 超过 100 万美元
  2. 低于 100 万美元
  3. 在 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>
4

1 回答 1

4

如果您想知道它是否高于 100 万,请将数字除以 100 万,然后使用 将数字四舍五入Math.floor()。如果答案高于零,那么这就是您拥有的“数百万”。然后你可以使用类似的东西插入单词百万(你需要在这里添加一些东西):

var val = $('your input').val()/1000000;
if (Math.floor(val) > 0) {
    $('your element with words').text( val + " million" );
}

对 1000 执行相同操作,但只需除以 1000 而不是 1000000。

于 2011-11-11T04:37:11.513 回答