2016-05-17 79 views
0

非常感謝您提供任何幫助......我對PHP和Javascript真的很陌生 - 但我很享受這種體驗。我有一個表格需要接受正整數,並使用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(); 
     } 
    }); 
    }); 

正如你可以看到這兩個控件是相同的......我想要做的是使按鍵功能的獨立功能,這樣我就可以定義表單輸入時,包括它....但我還不知道從哪裏開始。

對不起,如果我使用了錯誤的術語 - 但一直在學習。如果我設法解決(或者關閉),我會在這裏發佈答案。

回答

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); 
}); 
+0

非常感謝你的工作出色! –

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'); 
+0

非常感謝你傑出的作品! –