0

这可能是一些奇怪的问题,但这是我面临的问题。我有带有 ID 的文本框,例如:

"pax_9495237e-5c9e-489f-8700-2f82e211fd51__Age"
"pax_9495237e-9h7e-489f-8700-2f82e211fd51__Age"
"pax_9495237e-9k2e-489f-8700-2f82e211fd51__Age"

现在我想检查所有文本框是否包含__Age在最后,是否具有数值。如果不是数字(INT),即不允许使用字符(否。),则发出警报。请帮助我,我不知道该怎么做。我知道我有一个班级选项,但我想按 ID 做。

4

5 回答 5

0

如果您只需要__Age在末尾的任何输入具有非数字值时显示一个警报,那么您可以尝试以下操作:

if($("input[type='text'][id$='__Age']").filter(function(){
    return !isNaN($(this).val()); //leave only those inputs with nonNumeric value
}).length) //count their lenght - if it's > 0 then show alert
    alert('There are inputs with "nonNumeric" values!');
于 2013-06-06T13:30:44.480 回答
0

就像是:

$("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."
);

还!在此处的关键代码的一个大对象(充满较小的对象和数组)中找到一个非常完整的列表

于 2013-06-06T12:45:42.457 回答
0

要选择您的输入,您可以使用:

$('[id$="__Age"]')

然后知道是否是一个数字

$('[id$="__Age"]').each(function(){
    if(isNaN(parseInt($(this).val()))){
        alert(this,id + ' is not a number');
    }
})

小提琴:http: //jsfiddle.net/3B9Vp/

于 2013-06-06T12:47:34.510 回答
0

像这样试试

$(document).ready(function(){
    $("input[type='text'][id$='__Age']").each(function(){
        if(isNaN(parseInt($(this).val())))
            alert('Not an Integer');                
        else
            alert('It is an Integer');
    });
});

它将检查所有以 '__Age' 结尾的 id 文本框值

于 2013-06-06T12:44:24.757 回答
0

为了获得以“__Age”结尾的所有输入,您可以使用 jquery 选择器

jQuery( "[attribute$='value']" )

您可以在此处找到更多详细信息

因此,在您的场合,您可以致电:

$('input[id$="__Age"]'); 

将返回所有 id 以“__Age”结尾的文本框。

为了验证输入是否为数字,您可以检查每个文本框的值并使用JavaScript 中提供的isNaN函数

这是一个示例,其中有 3 个文本框,您调用一个函数来检查它们的当前值是否为数字。

于 2013-06-06T13:13:58.407 回答