直接看程式
// Compatible with keyup
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
$(function () {
// Init format after load
$(".numberOnly").each(function () {
$(this).val(numberWithCommas($(this).val()));
});
// Realtime format changing
$(".numberOnly").keyup(function (event) {
// skip for arrow keys
if (event.which >= 37 && event.which <= 40) return;
// format number
$(this).val(function (index, value) {
return value
.replace(/\D/g, "")
.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
});
});
});
$("#form1").submit(function () {
// Remove commas before submit
$(".numberOnly").each(function () {
$(this).html($(this).html().replace(/,/g, ""));
});
return true;
});
ref: Init part、Realtime typing part
沒有留言:
張貼留言