2016-09-30 52 views
0

我寫,以便限制上,其類型爲text所有輸入的用戶輸入下面列出的代碼。現在我想從此限制中排除ID爲allowall的所有輸入。我如何修改代碼來允許這個?jQuery的選擇輸入類型的文本,其中ID不等於

$('input[type=text]').keypress(function (e) { 
    var regex = new RegExp("^[a-zA-Z0-9 ]+$"); 
    var str = String.fromCharCode(!e.charCode ? e.which : e.charCode); 
    if (regex.test(str)) { 
     return true; 
    } 
    e.preventDefault(); 
    return false; 
}); 
+0

使用。不能選擇。 IT會幫助你。 –

回答

0
$('input[type=text]').not('#allowall').keypress(function (e) { 
    var regex = new RegExp("^[a-zA-Z0-9 ]+$"); 
    var str = String.fromCharCode(!e.charCode ? e.which : e.charCode); 
    if (regex.test(str)) { 
     return true; 
    } 
    e.preventDefault(); 
    return false; 
}); 
1

您可以使用屬性不等於選擇

$(":text[id!='allowall']").keypress(function (e) { 
    // do stuff with `<input type="text">` elements 
    // that do not have `id` equal to `"allowall"` 
}); 
1

檢查文本框的ID,並排除它們的正則表達式測試

$('input[type=text]').keypress(function (e) { 

    if($(this).attr('id') == 'allowall') 
    return true; 

    var regex = new RegExp("^[a-zA-Z0-9 ]+$"); 
    var str = String.fromCharCode(!e.charCode ? e.which : e.charCode); 
    if (regex.test(str)) { 
     return true; 
    } 
    e.preventDefault(); 
    return false; 
}); 
1

您也可以使用:not選擇,這應該會更快,因爲!=是jQuery選擇和:not是CSS選擇器。就像這樣:

$('input[type=text]:not(#allowall)').keypress(function(e){ 
    /*stuff*/ 
}) 
相關問題