2012-12-06 35 views
0

我想根據用戶輸入來計算一個字符串,並顯示一些消息:否則,如果在JavaScript

$('#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是不工作...我有錯過了什麼?

+3

''||應該是''&&其他 –

+3

問題是,如果你有長度的316不會在任何'if'聲明輸入文本。 –

回答

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'); 
}); 
+1

我想念那部分..謝謝現在工作..我需要睡覺... – rusly

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'); 

在世界上每一個數字比316小大於158 ,所以替代條件將永遠不會達到。