0

ユーザー入力に基づいて文字列を計算し、メッセージを表示しようとしています:

$('#myInput').keyup(function() {
    if(this.value.length<=158)
     $('#charCount').text('We will deduct 1 credit from your account');
    else if(this.value.length>158 || this.value.length<316)
         $('#charCount').text('We will deduct 2 credit from your account');
    else if(this.value.length>316 || this.value.length<400)
        $('#charCount').text('We will deduct 3 credit from your account');
});

http://jsfiddle.net/p9jVB/11/

問題は、最後else ifが機能していないことです... 何か見逃したことがありますか?

4

2 に答える 2

7

|| ではなく && のようです。

$('#myInput').keyup(function() {
    if(this.value.length<=158)
         $('#charCount').text('We will deduct 1 credit from your account');
    else if(this.value.length>158 && this.value.length<=316)
         $('#charCount').text('We will deduct 2 credit from your account');
    else if(this.value.length>316 && this.value.length<400)
        $('#charCount').text('We will deduct 3 credit from your account');
});
于 2012-12-06T09:51:18.297 に答える
4
else if(this.value.length>158 || this.value.length<316)
    $('#charCount').text('We will deduct 2 credit from your account');
else if(this.value.length>316 || this.value.length<400)
    $('#charCount').text('We will deduct 3 credit from your account');

世界のすべての数は 158 より大きい316 より小さいため、代替条件に到達することはありません。

于 2012-12-06T09:53:22.400 に答える