Loop through all text boxes in a form using jQuery

后端 未结 5 771
予麋鹿
予麋鹿 2020-12-16 18:01

I have a form which is laid out like a spreadsheet.

I want to validate the text in each textbox and if it\'s not numeric, change the background of the textbox and di

5条回答
  •  隐瞒了意图╮
    2020-12-16 18:49

    // locate all ":input" elements within a generic 
    , // then use .each() to loop through all results $('form :input').each(function(){ var $el = $(this); // element we're testing // attempt to cast the form's value to a number var n = parseFloat($el.val()); // check if the conversion passed if (isNaN(n)){ // $el does not have a number in it } });

    I think is what you're after. You can also specify input[type="text"] if you want to be more specific to

    Or, more concisely:

    $('form input[type="text"]').each(function(i,e){
        if (isNaN(parseFloat(e.value,10))){
            $(e).css('backgroundColor','red');
        }
    });
    

提交回复
热议问题