先感谢您的帮助......我是PHP和Javascript的新手 - 但我很享受这种体验。我在表单上有2个字段,只需要接受正整数并使用来自jQuery: what is the best way to restrict "number"-only input for textboxes? (allow decimal points)的代码和注释我能够提出以下代码,如果切换到包含小数位,也会检查用户只输入了1位小数:
$(document).ready(function() {
$("#NoOfClients").keypress(function(event) {
// Backspace, tab, enter, end, home, left, right
// We don't support the del key in Opera because del == . == 46.
// Add 46 to ControlKeys for allowing decimal point
var controlKeys = [8, 9, 13, 35, 36, 37, 39];
// IE doesn't support indexOf
var isControlKey = controlKeys.join(",").match(new RegExp(event.which));
var hasDecimalPoint = (($(this).val().split('.').length-1)>0);
// Some browsers just don't raise events for control keys. Easy.
// e.g. Safari backspace.
if (!event.which || // Control keys in most browsers. e.g. Firefox tab is 0
(49 <= event.which && event.which <= 57) || // Always 1 through 9
(48 == event.which && $(this).attr("value")) || // No 0 first digit
(isControlKey && !hasDecimalPoint)) { // Opera assigns values for control keys.
return;
} else {
event.preventDefault();
}
});
$("#NoOfClientContacts").keypress(function(event) {
// Backspace, tab, enter, end, home, left, right
// We don't support the del key in Opera because del == . == 46.
// Add 46 to ControlKeys for allowing decimal point
var controlKeys = [8, 9, 13, 35, 36, 37, 39];
// IE doesn't support indexOf
var isControlKey = controlKeys.join(",").match(new RegExp(event.which));
var hasDecimalPoint = (($(this).val().split('.').length-1)>0);
// Some browsers just don't raise events for control keys. Easy.
// e.g. Safari backspace.
if (!event.which || // Control keys in most browsers. e.g. Firefox tab is 0
(49 <= event.which && event.which <= 57) || // Always 1 through 9
(48 == event.which && $(this).attr("value")) || // No 0 first digit
(isControlKey && !hasDecimalPoint)) { // Opera assigns values for control keys.
return;
} else {
event.preventDefault();
}
});
});
正如你可以看到两个控件都是相同的...我想要做的是使按键功能成为一个独立的功能,所以我可以在定义表单输入时包含它....但我还没有一个线索从哪里开始。
很抱歉,如果我使用了错误的术语 - 但是一直在学习。如果我设法解决(或接近),我会在这里发布答案。
答案 0 :(得分:1)
.keypress
接收函数作为参数,可以发送命名函数指针
function handleKey(event) {
// Backspace, tab, enter, end, home, left, right
// We don't support the del key in Opera because del == . == 46.
// Add 46 to ControlKeys for allowing decimal point
var controlKeys = [8, 9, 13, 35, 36, 37, 39];
// IE doesn't support indexOf
var isControlKey = controlKeys.join(",").match(new RegExp(event.which));
var hasDecimalPoint = (($(this).val().split('.').length-1)>0);
// Some browsers just don't raise events for control keys. Easy.
// e.g. Safari backspace.
if (!event.which || // Control keys in most browsers. e.g. Firefox tab is 0
(49 <= event.which && event.which <= 57) || // Always 1 through 9
(48 == event.which && $(this).attr("value")) || // No 0 first digit
(isControlKey && !hasDecimalPoint)) { // Opera assigns values for control keys.
return;
} else {
event.preventDefault();
}
}
使用:
$(document).ready(function() {
$("#NoOfClients").keypress(handleKey);
$("#NoOfClientContacts").keypress(handleKey);
});
答案 1 :(得分:1)
function inputHandler(elem){
$(elem).keypress(function(event) {
// Backspace, tab, enter, end, home, left, right
// We don't support the del key in Opera because del == . == 46.
// Add 46 to ControlKeys for allowing decimal point
var controlKeys = [8, 9, 13, 35, 36, 37, 39];
// IE doesn't support indexOf
var isControlKey = controlKeys.join(",").match(new RegExp(event.which));
var hasDecimalPoint = (($(this).val().split('.').length-1)>0);
// Some browsers just don't raise events for control keys. Easy.
// e.g. Safari backspace.
if (!event.which || // Control keys in most browsers. e.g. Firefox tab is 0
(49 <= event.which && event.which <= 57) || // Always 1 through 9
(48 == event.which && $(this).attr("value")) || // No 0 first digit
(isControlKey && !hasDecimalPoint)) { // Opera assigns values for control keys.
return;
} else {
event.preventDefault();
}
});
}
您可以为多个输入调用此功能,如下所示 -
inputHandler('#NoOfClients');
inputHandler('#NoOfClientContacts');