博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Format Currency Sample - Format as you type
阅读量:6310 次
发布时间:2019-06-22

本文共 6073 字,大约阅读时间需要 20 分钟。

hot3.png

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Format Currency Sample - Format as you type</title>
<style type="text/css">
body, div { margin:0px auto; padding:0px; }
.main { margin:40px; }
.instructions { font-style:italic; }
.sample { float:left; margin:10px; padding:4px; border:1px solid #888; width:380px; min-height:100px; }
.sample h3 { margin:-4px; margin-bottom:10px; padding:4px; background:#555; color:#eee; }
.sample div { clear:both; }
.sample input { float:left; margin:0px auto; }
.message { color:#f00; font-size:.8em; }
.log { width:790px; clear:both; }
.log h3 { background:#966; }
#clearLog { float:right; }
</style>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript">
$(function() {
// jQuery formatCurrency plugin: http://plugins.jquery.com/project/formatCurrency
// Format while typing & warn on decimals entered, 2 decimal places
$('#formatWhileTypingAndWarnOnDecimalsEntered2').blur(function() {
$('#formatWhileTypingAndWarnOnDecimalsEnteredNotification2').html(null);
$(this).formatCurrency({ colorize: true, negativeFormat: '-%s%n', roundToDecimalPlace: 2 });
})
.keyup(function(e) {
var e = window.event || e;
var keyUnicode = e.charCode || e.keyCode;
if (e !== undefined) {
switch (keyUnicode) {
case 16: break; // Shift
case 17: break; // Ctrl
case 18: break; // Alt
case 27: this.value = ''; break; // Esc: clear entry
case 35: break; // End
case 36: break; // Home
case 37: break; // cursor left
case 38: break; // cursor up
case 39: break; // cursor right
case 40: break; // cursor down
case 78: break; // N (Opera 9.63+ maps the "." from the number key section to the "N" key too!) (See: http://unixpapa.com/js/key.html search for ". Del")
case 110: break; // . number block (Opera 9.63+ maps the "." from the number block to the "N" key (78) !!!)
case 190: break; // .
default: $(this).formatCurrency({ colorize: true, negativeFormat: '-%s%n', roundToDecimalPlace: -1, eventOnDecimalsEntered: true });
}
}
})
.bind('decimalsEntered', function(e, cents) {
if (String(cents).length > 2) {
var errorMsg = 'Please do not enter any cents (0.' + cents + ')';
$('#formatWhileTypingAndWarnOnDecimalsEnteredNotification2').html(errorMsg);
log('Event on decimals entered: ' + errorMsg);
}
});
// Warn on decimals entered, 2 decimal places
$('#warnOnDecimalsEntered2').blur(function() {
$('#warnOnDecimalsEnteredNotification2').html(null);
$(this).formatCurrency({ roundToDecimalPlace: 2, eventOnDecimalsEntered: true });
})
.bind('decimalsEntered', function(e, cents) {
var errorMsg = 'Please do not enter any cents (0.' + cents + ')';
$('#warnOnDecimalsEnteredNotification2').html(errorMsg);
log('Event on decimals entered: ' + errorMsg);
});
// Format while typing & warn on decimals entered, no cents
$('#formatWhileTypingAndWarnOnDecimalsEntered').blur(function() {
$('#formatWhileTypingAndWarnOnDecimalsEnteredNotification').html(null);
$(this).formatCurrency({ colorize: true, negativeFormat: '-%s%n', roundToDecimalPlace: 0 });
})
.keyup(function(e) {
var e = window.event || e;
var keyUnicode = e.charCode || e.keyCode;
if (e !== undefined) {
switch (keyUnicode) {
case 16: break; // Shift
case 27: this.value = ''; break; // Esc: clear entry
case 35: break; // End
case 36: break; // Home
case 37: break; // cursor left
case 38: break; // cursor up
case 39: break; // cursor right
case 40: break; // cursor down
case 78: break; // N (Opera 9.63+ maps the "." from the number key section to the "N" key too!) (See: http://unixpapa.com/js/key.html search for ". Del")
case 110: break; // . number block (Opera 9.63+ maps the "." from the number block to the "N" key (78) !!!)
case 190: break; // .
default: $(this).formatCurrency({ colorize: true, negativeFormat: '-%s%n', roundToDecimalPlace: -1, eventOnDecimalsEntered: true });
}
}
})
.bind('decimalsEntered', function(e, cents) {
var errorMsg = 'Please do not enter any cents (0.' + cents + ')';
$('#formatWhileTypingAndWarnOnDecimalsEnteredNotification').html(errorMsg);
log('Event on decimals entered: ' + errorMsg);
});
// Warn on decimals entered, no cents
$('#warnOnDecimalsEntered').blur(function() {
$('#warnOnDecimalsEnteredNotification').html(null);
$(this).formatCurrency({ roundToDecimalPlace: 0, eventOnDecimalsEntered: true });
})
.bind('decimalsEntered', function(e, cents) {
var errorMsg = 'Please do not enter any cents (0.' + cents + ')';
$('#warnOnDecimalsEnteredNotification').html(errorMsg);
log('Event on decimals entered: ' + errorMsg);
});
function log(text) {
$('#divLog').prepend('<div>' + text + '</div>');
}
$('#clearLog').click(function() {
$('#divLog').empty();
});
});
</script>
</head>
<body>
<div class="main">
<h1>Format Currency Sample - <span style="font-size:.7em">Format as you type</span></h1>
<p class="instructions">
Type "123456.789" into the fields below to see the formatting
</p>
<div class="sample">
<h3>Round to 2 decimal places</h3>
<div>Format while typing & warn on decimals entered</div>
<input type="text" id="formatWhileTypingAndWarnOnDecimalsEntered2" />
<span id="formatWhileTypingAndWarnOnDecimalsEnteredNotification2" class="message"></span>
<div>Warn on decimals entered</div>
<input type="text" id="warnOnDecimalsEntered2" />
<span id="warnOnDecimalsEnteredNotification2" class="message"></span>
</div>
<div class="sample">
<h3>No Decimals</h3>
<div>Format while typing & warn on decimals entered</div>
<input type="text" id="formatWhileTypingAndWarnOnDecimalsEntered" />
<span id="formatWhileTypingAndWarnOnDecimalsEnteredNotification" class="message"></span>
<div>Warn on decimals entered</div>
<input type="text" id="warnOnDecimalsEntered" />
<span id="warnOnDecimalsEnteredNotification" class="message"></span>
</div>
<div class="sample log">
<input type="button" id="clearLog" value="clear" />
<h3>Log</h3>
<br />
<div id="divLog"></div>
</div>
</div>
</body>
</html>

转载于:https://my.oschina.net/u/1047983/blog/138269

你可能感兴趣的文章
VUE高仿饿了么app
查看>>
针对Kubernetes软件栈有状态服务设计的思考
查看>>
你的可用性达标了吗?云端业务性能高可用的深度实践
查看>>
linux yum清缓存脚本
查看>>
基于epoll封装的事件回调miniserver
查看>>
天猫高管全面解读大快消2018新零售打法
查看>>
idea springboot热部署无效问题
查看>>
第八章 进程间通信
查看>>
HttpSession接口中的方法(Jsp中的session类的用法)
查看>>
「镁客早报」AI可预测心脏病人死亡时间;机器人开始在美国送外卖
查看>>
MoQ(基于.net3.5,c#3.0的mock框架)简单介绍
查看>>
物联网全面升级,十年内推动工业进入智能化新阶段
查看>>
spring-通过ListFactory注入List
查看>>
一种基于SDR实现的被动GSM嗅探
查看>>
阿里云ECS每天一件事D1:配置SSH
查看>>
SQL Server 性能调优(性能基线)
查看>>
uva 10801 - Lift Hopping(最短路Dijkstra)
查看>>
[Java Web]servlet/filter/listener/interceptor区别与联系
查看>>
POJ 2312Battle City(BFS-priority_queue 或者是建图spfa)
查看>>
从零开始学MVC3——创建项目
查看>>