HTML:Javascript數組沒有得到陣列結果
<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");
}
}
我已經打電話tournamentPrize()函數的onblur這個領域,當我輸入任意數目是沒有顯示正確的結果。總是顯示-1
我想得到我正確的結果,當我將輸入2,4,8,16,32,64和128但如果我輸入任何數字除了上面的數字結果也顯示錯誤。
假設我想進入3警報框將來到警報(「你輸入錯號」);但它顯示警報(「您已輸入3號碼」);
請有人幫助我
應該是'如果(myArray.indexOf(field_val)!= = -1)' – Sarath
也許你也在爲'Internet Explorer的<.indexOf()'搜索一個polyfill <9:htt ps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf – WoIIe
我使用Mozilla Firefox 26.0 – Chinmay235