1

Javascript を使用して Titanium でアプリを開発しており、次のチェック ロジックを実現しようとしています:
ユーザーは 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。2 番目の値 - 5;

2) 最初の値 - 105; 2 番目の値 - 101;

そして主なことは、最初の値が >= 100 であるが、2 番目の値が < 100 である場合 -それは機能しません

条件は正しいようですが、正しく動作しません - 間違いが見つかりません。

4

1 に答える 1

1

あなたが抱えている問題は、値を数値ではなく文字列として比較していることだと思います。2 つの文字列を比較する場合、Javascript は順番に文字の Unicode 値に基づいて比較します。それはあなたにとって何を意味しますか?短い答えは、 while"90" < 200trueその比較の結果が"90"に強制されるためであり90、は より大きいためです。"90" < "200"false"9""2"

この動作を回避するには、変数の一方または両方を数値に変換する必要があります。文字列を数値に変換するこの回答は、それを行うためのいくつかの方法を示していますが、あなたの場合、それparseInt(input_text.value, 10) <= parseInt(value_alert, 10)はうまくいくと思います.

于 2013-11-03T20:36:58.413 に答える