3

我写了一个代码片段,它适用于除零以外的所有值。它不将零视为数字并将其发送到 else 条件。所以验证对我来说失败了。我附上了下面的代码。所有非零数字都被接受,但是当我在文本框中写零时,根据我的验证它说“请输入一个数字”。

$('.scoreTextInput').change(function(){
            $(this).each(function(){
                var errorId=$(this).attr('bind');
                if(Number($(this).val()))
                {
                    if(($(this).val())>100)
                    {
                    $('#'+errorId).text('Score must not exceed 100%');
                    $(this).val(0);
                    }
                    else{
                        $('#'+errorId).text('');

                    }
                }
                    else
                    {
                    $('#'+errorId).text('Please enter a number');
                    $(this).val(0); 
                    }
                }); 
4

1 回答 1

0

也许试试这个?

if($.isNumeric($(this).val()))

jQuery .isNumeric

于 2012-09-06T22:32:58.853 回答