gpt4 book ai didi

javascript - 创建多次使用的函数

转载 作者:行者123 更新时间:2023-12-02 14:37:56 26 4
gpt4 key购买 nike

提前感谢您提供的任何帮助...我对 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();
}
});
});

正如你所看到的,两个控件是相同的...我想做的是使按键功能成为一个独立的功能,这样我就可以在定义表单输入时包含它...但我没有任何线索从哪里开始。

抱歉,如果我使用了错误的术语 - 但我一直在学习。如果我设法解决(或接近),我将在这里发布答案。

最佳答案

.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);
});

关于javascript - 创建多次使用的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37274806/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com