【发布时间】:2017-09-29 10:06:02
【问题描述】:
我有这样的看法
<div class="form-group">
<div class="col-sm-4 col-md-4">
<label>Harga Normal</label>
<input id="NormalPrice" type="text" size="30" class="form-control number uang" name="NormalPrice" value="">
</div>
<div class="col-sm-4 col-md-4">
<label>Harga Diskon</label>
<input id="DiscountPrice" type="text" size="30" class="form-control number" name="DiscountPrice" value="">
</div>
<div class="col-sm-4 col-md-4">
<label>Hari aktif kupon</label>
<input id="CouponActiveDay" type="text" min="0" max="365" class="form-control number" name="CouponActiveDay" value="">
</div>
</div>
我想让文本变成数字,所以我是这样使用的
<script src="//cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>
function count(){
var res = (Number($('#NormalPrice').val()) - Number($('#DiscountPrice').val())) / Number($('#NormalPrice').val()) * 100;
$('#Discount').val(parseInt(res)+"%");
}
$('#DiscountPrice').keyup(count);
$(document).on('keypress','.number',function(e){
if((e.which <= 57 && e.which >= 48) || (e.keyCode >= 37 && e.keyCode <= 40) || e.keyCode==9 || e.which==43 || e.which==44 || e.which==45 || e.which==46 || e.keyCode==8){
return true;
}else{
return false;
}
});
$(document).on('blur','.uang',function(){
$(this).val(numeral($(this).val()).format('0,0'));
}).on('focus','.uang',function(){
$(this).val($(this).val());
});
但是当我在 NormalPrice 上输入不超过 3 位数字时.. 我可以获得折扣值.. 但如果我输入超过 3 位数字,我会得到 NaN,我认为它返回字符串,但我使用了 parseInt。
【问题讨论】:
-
@Pete 我用过 parseInt()。但它返回 NaN
-
问题:
$(this).val(numeral($(this).val()).format('0,0'));(以及为什么$(this).val($(this).val());o.O) -
@Andreas 模糊时你有其他方法来获取数字格式吗?我正在使用
$(this).val(Number($(this).val()));如果我只使用Number($(this).val()).. 当我聚焦时,我的文本字段将显示 NaN -
如果你输入
1000,它会变成1,000,而Number('1,000')就是NaN。无论如何,您是否要在表单中添加,。这会导致很多问题。
标签: jquery numbers number-formatting