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;第二个值 - 5;

2) 第一个值 - 105;第二个值 - 101;

最重要的是,如果第一个值是 >= 100 ,但第二个值是 < 100 -它不起作用

似乎条件是正确的,但工作不正确 - 找不到错误。

4

1 回答 1

1

我认为您遇到的问题是您将值作为字符串而不是数字进行比较。当您比较两个字符串时,Javascript 会根据字符的 Unicode 值按顺序进行比较。这对你意味着什么?简短的回答是,虽然"90" < 200true因为比较导致"90"被强制为90"90" < "200"false因为"9"大于"2"

为了避免这种行为,您需要将一个或两个变量转换为数字。这个关于将字符串转换为数字的答案为您提供了多种方法,但在您的情况下,我认为这parseInt(input_text.value, 10) <= parseInt(value_alert, 10)对您来说很好。

于 2013-11-03T20:36:58.413 回答