0

これは奇妙な質問かもしれませんが、私が直面している問題です。次のような Id の textBox があります。

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

ここで、すべての Textboxes__Ageが最後に構成されているかどうか、数値があるかどうかを確認したいと思います。数値 (INT)、つまり文字 (no .) が許可されていない場合は、警告を発します。どうすればいいのかわからないので助けてください。クラスオプションがあることは知っていますが、IDで行いたいです。

4

5 に答える 5

0

「__Age」で終わるすべての入力を取得するには、jqueryセレクターを使用できます

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

詳細はこちら

だから、あなたの機会にあなたは呼び出すことができます:

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

「__Age」で終わる ID を持つすべてのテキスト ボックスが返されます。

入力が数値かどうかを検証するには、各テキスト ボックスの値を確認し、Javascript で提供されるisNaN関数を使用します。

以下は、3 つのテキスト ボックスがあり、関数を呼び出して現在の値が数値かどうかを確認する例です。

于 2013-06-06T13:13:58.407 に答える
0

__Age末尾にある入力のいずれかに数値以外の値が含まれている場合にアラートを 1 つだけ表示する必要がある場合は、次のようなことを試すことができます。

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

入力を選択するには、次を使用できます。

$('[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

何かのようなもの:

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

また!キーコードの 1 つの大きなオブジェクト (小さなオブジェクトと配列でいっぱい) のかなり完全なリストをここで見つけてください。

于 2013-06-06T12:45:42.457 に答える