javascript - 在输入类型文本中将小数点右对齐

标签 javascript html css

我想将数字格式化为小数点后两位,还想对齐,即不希望某些数字居中,某些左对齐等。 我不知道这有多容易,但想在小数点上对齐。有点像这样:

 123.44
1465.23
  12.24

只要所有数字有两个 dp,右对齐就可以。不希望数字看起来像这样:

1554
23.75

它们应该是这样的:

1554.00
  23.75

HTML代码

 <table class="table grey-table">
    <thead>
        <tr>
            <th style="padding:1px 8px;">Description</th>
            <th style="padding:1px 8px;">Total</th>
        </tr>
    </thead>
    <tr>
    <td style="padding:1px 8px;">Countertops</td>
    <td style="width:150px;"><input type="text" id="txt_cm_countertops" readonly="true" style="width:150px;"/></td>
    </tr>
    <tr>
    <td style="padding:1px 8px;">Li In.</td>
    <td style="width:150px;"><input type="text" id="txt_cm_countertops_lf" readonly="true" style="width:150px;"/></td>
    </tr>
    <tr>
    <td style="padding:1px 8px;">Bowls</td>
    <td style="width:150px;"><input type="text" id="txt_cm_bowls" readonly="true" style="width:150px;"/></td>
    </tr>
    <tr>
    <td style="padding:1px 8px;">Side Splashes</td>
    <td style="width:150px;"><input type="text" id="txt_cm_sidesplashes" readonly="true" style="width:150px;"/></td>
    </tr>
    <tr>
    <td style="padding:1px 8px;">Subtops</td>
    <td style="width:150px;"><input type="text" id="txt_cm_subtops" readonly="true" style="width:150px;"/></td>
    </tr>
  </table>

<sript>
var I = 24; //These values are for demo. These are not hardcorded
var J = 15.5;
var K = 4;
var L = 4;
var M = 21.4;

$("#txt_cm_countertops").val(I);    
$("#txt_cm_countertops_lf").val(J); 
$("#txt_cm_bowls").val(K);  
$("#txt_cm_sidesplashes").val(L);   
$("#txt_cm_subtops").val(M);

</script>

这是上表的图像想要对齐具有enter image description here的总列

最佳答案

您将不得不使用 css text-align: right 属性和 JavaScript 掩码。

这个 jQuery 插件是我很幸运的一个:

http://digitalbush.com/projects/masked-input-plugin/

例子:

<script src="jquery.js" type="text/javascript"></script>
<script src="jquery.maskedinput.js" type="text/javascript"></script>
<script>
$( document ).ready(function() {
  // Find a field with an ID of 'myNum' and mask it display a two digit number
  $("#myNum").mask("9.99");
  // Add mask of two digit number to anything with a class of 'twodigit'
  $(".twodigit").mask("9.99");
  // Really old browsers don't like the class method so you have to run a for/each
  // loop by class name
  $(".twodigit").each(function(){
     $(this).mask("9.99");
  });

});
</script>

关于javascript - 在输入类型文本中将小数点右对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22893911/

相关文章:

html - 谷歌搜索引擎优化索引子域

html - 来自与 youtube 不同的来源的视频进入花式盒

html - 截掉元素的部分内容

javascript - 如何允许在页面上只播放一个嵌入的 YouTube 视频?

javascript - knockout : how to filter by genre AND by mediatype

javascript - 当我重新加载 div 时,Google 折线图不会重新加载

javascript - 通过单击图像显示/隐藏 div

javascript - 如何从日期时间本地删除清除按钮?

html - 使用 Bootstrap 进行页面布局

javascript - 如何进行随机洗牌?