2013-11-03 2 views
1

자바를 사용하여 티타늄에서 앱을 개발 중이며 다음 확인 로직을 구현하려고합니다 :
사용자가 1과 200 사이의 값 하나를 입력 한 다음/그녀는 1과 첫 번째 값 사이의 범위에있는 두 번째 값을 입력해야합니다 (작거나 같지만 첫 번째 값은 더 이상 없습니다). 예를 들어티타늄/자바 스크립트 범위의 수를위한 조건

var value_alert = ''; //First value 
var value_remind = ''; //Second value (should be less or equal) 

var default_value_alert = 10; //Default first value for TextField 
var default_value_remind = 5; //Default second value for TextField 

// handle and save the first value entered by user 
function doOpenAlert(){ 

    var input_text = Ti.UI.createTextField({ 
     keyboardType: Ti.UI.KEYBOARD_PHONE_PAD 
    }); 

    if(value_alert === ''){ 
     input_text.value = default_value_alert;  

    } else { 
     input_text.value = value_alert; 
    } 

    var dialog = Ti.UI.createOptionDialog({ 
     title : "Specified distance in the range 1-200 km", 
     androidView : input_text, 
     buttonNames : ['Ok', 'Cancel'] 
    }); 
    dialog.show();  
    dialog.addEventListener('click', function(e){ 

     if(value_remind === ''){ 
      value_remind = default_value_remind;  
     } 

     if(e.index == 0){ // Check is Ok pressed 
      // check_number = isInt(input_text.value); 

      if(input_text.value >= 1 && input_text.value <= 200){ // Check that the first value is in range 
       var toast = Titanium.UI.createNotification({ 
        duration: 2000, 
        message: "Distance is " + input_text.value + " km." 
       }); 
       toast.show(); 
       value_alert = input_text.value; // Saving the first value entered by user 
      } else if(input_text.value == 0){ 
       alert("The field is empty."); 
      } else if(!(input_text.value >= 1 && input_text.value <= 200)){ 
       alert("Range is between 1 and 200 km."); 
      } 
     } 
    }); 
} 

// handle and save the second value entered by user 
function doOpenMinne(){ 

    var input_text = Ti.UI.createTextField({ 
     keyboardType: Ti.UI.KEYBOARD_PHONE_PAD 
    }); 

    if(value_remind === ''){ 
     input_text.value = default_value_remind; 
    } else { 
     input_text.value = value_remind; 
    } 

    var dialog = Ti.UI.createOptionDialog({ 
     title : "Remind before number", 
     androidView : input_text, 
     buttonNames : ['Ok', 'Cancel'] 
    }); 
    dialog.show(); 
    dialog.addEventListener('click', function(e){ 

     if(value_alert === ''){ 
      value_alert = default_value_alert;  
     } 

     if(e.index == 0){ 
      // check_number = isInt(input_text.value); 
      if(input_text.value >= 1 && input_text.value <= value_alert){ // Check if the second value in is range between 1 and the first value 
       var toast = Titanium.UI.createNotification({ 
        duration: 2000, 
        message: "Remind at " + input_text.value + " km." 
       }); 
       toast.show(); 
       value_remind = input_text.value; // Saving the second value entered by user 
      } else if(input_text.value == 0){ 
       alert("The field is empty"); 
      } else if(!(input_text.value >= 1 && input_text.value <= 200)){ 
       alert("The range is between 1 and 200 km"); 
      } 
     } 
    }); 
} 

, 그것은 다음과 같은 조합에서 잘 작동 : 여기

내 코드입니다
1) 첫 번째 값 - (10); 두 번째 값 - 5;

2) 첫 번째 값 - 105; 두 번째 값 - 101;

첫 번째 값이 100보다 크지 만 두 번째 값이 < 100 - 인 경우 중요한 것은 작동하지 않습니다..

조건은 정확하지만 잘못 작동하는 것 같습니다. 실수를 찾을 수 없습니다.

답변

1

저는 문제는 숫자가 아닌 문자열로 값을 비교한다는 것입니다. 두 문자열을 비교하면 Javascript bases the comparison on the Unicode values of the characters in order입니다. 그게 너에게 의미가 있니? 짧은 대답이 "90" < 200true를 동안 "90"에서 그 비교 결과가 "9""2"보다 커서는 "90" < "200"false이다 90로 강제되고 있기 때문이다.

이 문제를 방지하려면 변수 중 하나 또는 둘 모두를 숫자로 변환해야합니다. This answer on converting strings into numbers에는 여러 가지 방법이 나와 있지만, 귀하의 경우에는 parseInt(input_text.value, 10) <= parseInt(value_alert, 10)이 잘 작동한다고 생각합니다.

+0

에반 감사합니다! 당신은 맞습니다 - 다른 유형입니다. 이제 제대로 작동합니다. – devger

관련 문제