【发布时间】:2017-07-31 04:01:18
【问题描述】:
我在这里发疯了...我已经投入了超过 16 个小时试图让这件事起作用,如果有人知道这种邪恶行为的原因,请指教..
我在一个表单中有多个类型为 number 的输入,我将键盘连接到每个输入,如下所示:
$('.amount').keyboard({
layout: 'custom',
customLayout : {
'normal' : ['1 2 3', '4 5 6', '7 8 9','{clear} 0 {bksp}','{accept}']
},
display : {
'accept' : 'Confirm:Confirm (Shift-Enter)',
'bksp' : 'Delete:Delete',
'clear': 'Clear:Clear'
},
beforeVisible: function(e, keyboard, el) {
$("#"+keyboard.$keyboard[0].id).addClass("hide-me");
},
restrictInput: true,
preventPaste: true,
autoAccept: true,
usePreview: false,
useWheel: false,
repeatRate: 0,
// this function is so I can display cents in the input
// there is no decimal in the keyboard as part of the requirements
change: function(e, keyboard, el) {
if (el.value === null || el.value === "") {
el.value = "0.00";
}
el.value = parseInt(el.value);
el.value = el.value * 0.01;
},
// this function is so I can display cents in the inputs
// there is no decimal in the keyboard as part of the requirements
accepted: function(e, keyboard, el) {
if (el.value === null || el.value === "") {
el.value = "0.00";
}
el.value = parseInt(el.value);
el.value = el.value * 0.01;
},
caretToEnd: 'true',
maxLength : '20',
css: {
container: 'center-block dropdown-menu custom-keypad',
buttonDefault: 'btn-kb',
buttonHover: 'btn-primary',
buttonAction: 'active',
buttonDisabled: 'disabled'
}
});
键盘按原样弹出,我可以在输入和确认输入时正确看到输入中的小数。我的目标是汇总所有 input.val() 并在表单发生任何更改时立即验证它们。这样做的功能是这样的:
$('form').on('change', '.amount', function () {
var balance = NUMBER;
var sum = 0;
$(".amount").each(function (){
var valTotal = Number($(this).val());
if (!isNaN(valTotal)) {
sum += valTotal;
}
});
if (sum > balance) {
//stuff happens
}
});//.end of form
这就是问题开始的地方,当我对输入求和时,我的小数消失了! 22.22 现在是 2222,所以我的总和是错误的,导致我的总和总是大于我的余额。我试图创建一个小提琴,但它不会在结果框中弹出键盘,所以我不能给你展示一个活生生的例子..
这是我正在使用的 CDN:
https://cdnjs.cloudflare.com/ajax/libs/virtual-keyboard/1.26.17/js/jquery.keyboard.min.js
请指教!!
【问题讨论】:
标签: javascript jquery jquery-ui keyboard uikeyboard