Javascript无法处理所有输入
我有一个脚本,可以管理哪些键可以按下,哪些键不能按下,一切都很好,但它只适用于一个输入。。。所以第一个! 如何让它在多个输入下工作Javascript无法处理所有输入,javascript,numbers,Javascript,Numbers,我有一个脚本,可以管理哪些键可以按下,哪些键不能按下,一切都很好,但它只适用于一个输入。。。所以第一个! 如何让它在多个输入下工作 First input <input type="text" id="inputs" /><br> Second input <input type="text" id="inputs" /> <script type='text/javascript'> toHtmlNumericInput('input
First input <input type="text" id="inputs" /><br>
Second input <input type="text" id="inputs" />
<script type='text/javascript'>
toHtmlNumericInput('inputs');
// call this function with the id of the input textbox you want to be html-numeric-input
// by default, decimal separator is '.', you can force to use comma with the second parameter = true
function toHtmlNumericInput(inputElementId, useCommaAsDecimalSeparator) {
var textbox = document.getElementById(inputElementId);
// called when key is pressed
// in keydown, we get the keyCode
// in keyup, we get the input.value (including the charactor we've just typed
textbox.addEventListener("keydown", function _OnNumericInputKeyDown(e) {
var key = e.which || e.keyCode; // http://keycode.info/
if (!e.shiftKey && !e.altKey && !e.ctrlKey &&
// alphabet
key >= 65 && key <= 90 ||
// spacebar
key == 32) {
e.preventDefault();
return false;
}
if (!e.shiftKey && !e.altKey && !e.ctrlKey &&
// numbers
key >= 48 && key <= 57 ||
// Numeric keypad
key >= 96 && key <= 105 ||
// allow: Ctrl+A
(e.keyCode == 65 && e.ctrlKey === true) ||
// allow: Ctrl+C
(key == 67 && e.ctrlKey === true) ||
// Allow: Ctrl+X
(key == 88 && e.ctrlKey === true) ||
// allow: home, end, left, right
(key >= 35 && key <= 39) ||
// Backspace and Tab and Enter
key == 8 || key == 9 || key == 13 ||
// Del and Ins
key == 46 || key == 45) {
return true;
}
var v = this.value; // v can be null, in case textbox is number and does not valid
// if minus, dash
if (key == 109 || key == 189) {
// if already has -, ignore the new one
if (v[0] === '-') {
// console.log('return, already has - in the beginning');
return false;
}
}
if (!e.shiftKey && !e.altKey && !e.ctrlKey &&
// comma, period and numpad.dot
key == 190 || key == 188 || key == 110) {
// console.log('already having comma, period, dot', key);
if (/[\.,]/.test(v)) {
// console.log('return, already has , . somewhere');
return false;
}
}
});
textbox.addEventListener("keyup", function _OnNumericInputKeyUp(e) {
var v = this.value;
if(false) {
// if (+v) {
// this condition check if convert to number success, let it be
// put this condition will have better performance
// but I haven't test it with cultureInfo = comma decimal separator, so, to support both . and , as decimalSeparator, I remove this condition
// "1000" "10.9" "1,000.9" "011" "10c" "$10"
//+str, str*1, str-0 1000 10.9 NaN 11 NaN NaN
} else if (v) {
// refine the value
// this replace also remove the -, we add it again if needed
v = (v[0] === '-' ? '-' : '') +
(useCommaAsDecimalSeparator ?
v.replace(/[^0-9\,]/g, '') :
v.replace(/[^0-9\.]/g, ''));
// remove all decimalSeparator that have other decimalSeparator following. After this processing, only the last decimalSeparator is kept.
if(useCommaAsDecimalSeparator){
v = v.replace(/,(?=(.*),)+/g, '');
} else {
v = v.replace(/\.(?=(.*)\.)+/g, '');
}
this.value = v; // update value only if we changed it
}
});
}
</script>
第一次输入
第二输入
toHtmlNumericInput(“输入”);
//使用要成为html数字输入的输入文本框的id调用此函数
//默认情况下,十进制分隔符为“.”,您可以强制使用逗号和第二个参数=true
函数到HTMLnumeriInput(inputElementId,使用CommaasDecimalSeparator){
var textbox=document.getElementById(inputElementId);
//按此键时调用
//在keydown中,我们得到了keyCode
//在keyup中,我们得到input.value(包括刚才键入的字符)
textbox.addEventListener(“keydown”,函数_onNumericiInputkeydown(e){
var key=e.which | e.keyCode;//http://keycode.info/
如果(!e.shiftKey&&!e.altKey&&!e.ctrlKey&&
//字母表
key>=65&&key=48&&key=96&&key=35&&key您的输入字段具有相同的ID,它们的ID应该是唯一的。为字段提供唯一的ID,然后为每个ID调用toHtmlNumericInput
函数。您的输入字段具有相同的ID,它们的ID应该是唯一的。为字段提供唯一的ID,然后调用函数>toHtmlNumericInput
函数用于每个ID。我尝试了例如:'toHtmlNumericInput('first','second');'但它不起作用,我被卡住了:(函数的设置方式是,你必须为每个输入字段分别调用它,像这样toHtmlNumericInput('first');toHtmlNumericInput('second'))
u很简单,很抱歉浪费时间:/I我只添加了一行,现在就可以使用了,谢谢,3分钟后我可以标记您的答案已解决:-)我尝试了例如:'toHtmlNumericInput('first','second');'但它不起作用我卡住了:(函数的设置方式是,您必须为每个输入字段分别调用它,例如toHtmlNumericInput('first');toHtmlNumericInput('second');
ouu很简单,很抱歉浪费时间:/I我只添加了一行,所以它现在可以工作了,谢谢,3分钟后我可以标记您的答案:-)