Javascript数组没有得到数组结果

时间:2014-01-21 07:06:07

标签: javascript jquery arrays

HTML:

<input type="text" onblur="tournamentPrize(this.value);" placeholder="Player Number" id="player_number" name="data[Tournament][player_number]"> 

Javascript:

function tournamentPrize(v){
        var myArray=[2, 4, 8, 16, 32, 64, 128];
        var field_val = v;
        if(myArray.indexOf(field_val)==-1){ //alert(myArray.indexOf(field_val));
            if(field_val==2){
                $('#_1st_prize').removeAttr('disabled');                
                $('#_2nd_prize').attr('disabled','disabled');
                $('#_2nd_prize').val('');               
                $('#_3rd_prize').attr('disabled','disabled');
                $('#_3rd_prize').val('');
                $('#_4th_prize').attr('disabled','disabled');
                $('#_4th_prize').val('');               
                $('#rest_prize').attr('disabled','disabled');
                $('#rest_prize').val('');
            }else if(field_val==4){
                $('#_1st_prize').removeAttr('disabled');                
                $('#_2nd_prize').removeAttr('disabled');            
                $('#_3rd_prize').attr('disabled','disabled');
                $('#_3rd_prize').val('');
                $('#_4th_prize').attr('disabled','disabled');
                $('#_4th_prize').val('')
                $('#rest_prize').removeAttr('disabled');
            }else{
                           alert("You have entered "+field_val+" number");
            }
        }
        else{
            alert("You have entered wrong number"); 
        }
    }

我在onblur这个字段中调用了tournamentPrize()函数,当我输入任何数字时它没有显示正确的结果。始终显示 -1

当我输入2,4,8,16,32,64和128时,我想获得正确的结果但是如果输入除上面数字以外的任何数字,结果也会显示错误。

假设我要输入3警告框将来 警告(“您输错了号码”); 但它显示 警告(“您输入了3个号码”);

请有人帮助我

3 个答案:

答案 0 :(得分:2)

function tournamentPrize(v){
        var myArray=[2, 4, 8, 16, 32, 64, 128];

        var field_val = +v; // convert value to number from string
        // OR var field_val = parseInt(v); OR var field_val = Number(v);

        if(myArray.indexOf(field_val) !== -1){ //entered valid value in array
            if (field_val==2) {
               ....
            } else if(field_val==4){
               .....
            } else{
               alert("You have entered "+field_val+" number");
            }
        }
        else{
            alert("You have entered wrong number"); 
        }
}

答案 1 :(得分:2)

您可以使用 document.getElementById onblur我创建了 JSFiddle

答案 2 :(得分:0)

 function tournamentPrize(v){
    var myArray=[2, 4, 8, 16, 32, 64, 128];
    var field_val = v;
    if(myArray.indexOf(field_val)!=-1){ //alert(myArray.indexOf(field_val));
        if(field_val==2){
            $('#_1st_prize').removeAttr('disabled');                
            $('#_2nd_prize').attr('disabled','disabled');
            $('#_2nd_prize').val('');               
            $('#_3rd_prize').attr('disabled','disabled');
            $('#_3rd_prize').val('');
            $('#_4th_prize').attr('disabled','disabled');
            $('#_4th_prize').val('');               
            $('#rest_prize').attr('disabled','disabled');
            $('#rest_prize').val('');
        }else if(field_val==4){
            $('#_1st_prize').removeAttr('disabled');                
            $('#_2nd_prize').removeAttr('disabled');            
            $('#_3rd_prize').attr('disabled','disabled');
            $('#_3rd_prize').val('');
            $('#_4th_prize').attr('disabled','disabled');
            $('#_4th_prize').val('')
            $('#rest_prize').removeAttr('disabled');
        }else{
                       alert("You have entered "+field_val+" number");
        }
    }
    else{
        alert("You have entered wrong number"); 
    }
}