就像是:
$("textarea").filter(function(i) { return $(this).prop("id").indexOf("__Age") !== -1; })
http://api.jquery.com/filter/
http://api.jquery.com/jQuery.inArray/
JavaScript 中如何检查字符串是否包含子字符串?
// an array of e.which|e.key codes that is every key code for every number and control (like shift, backspace, etc..)
var numsNcontrols = [48,49,50,51,52,53,54,55,56,57,96,97,98,99,100,101,102,103,104,105,8,9,13,16,17,18,19,20,32,33,34,35,36,45,46,44,145,37,38,39,40];
// first grab all inputs ending with "__Age", then asign "keydown" check for improper characters
$("input[type=text]").filter(function(i) { return $(this).prop("id").indexOf("__Age") !== -1; }).on("keydown", function(e) {
if ($.inArray(e.which, numsNcontrols) == -1) return false;
}) // now, using jQuery Chaining, we continue and asign keyup event to alert users they cannot enter anything but numbers
.on("keyup", function(e) {
if ($.inArray(e.which, numsNcontrols) == -1) alert('Numbers only please!');
})
// simple check value update to show how many textboxes end in "__Age"
$("#bil").val(
$("input[type=text]").filter(function(i) { return $(this).prop("id").indexOf("__Age") !== -1; }).length
+ " textboxes have __Age at end of ID."
);
// Shows in a textbox how many textboxes end in "__Age" && have a numeric value
$("#bad").val(
$("input[type=text]").filter(function(i) { return $(this).prop("id").indexOf("__Age") !== -1 && !isNaN(parseInt($(this).val())); }).length
+ " textboxes have __Age at end of ID && numeric value."
);
还!在此处的关键代码的一个大对象(充满较小的对象和数组)中找到一个非常完整的列表