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 回答