0

2 つの非表示の入力フィールドで複数のチェックを行う小さなスクリプトがあります。

    function checkfs()
{ 
var rating1 = (document.getElementById("rating").value);
var rating2 = (document.getElementById("rating").value);
var rating3 = (document.getElementById("rating").value);
var check1 = (document.getElementById("countpl").value);
var check2 = (document.getElementById("countpl").value);
var check3 = (document.getElementById("countpl").value);

    if (rating3 == 3 && check3 > 22 || check3 < 19){
        alert("message 1");
        window.location.href = 'myteam.php';}

else if (rating2 == 2 && check2 > 21 || check2 < 18){
        alert("message 2");
        window.location.href = 'myteam.php';}

else if (rating1 == 1 && check1 > 20 || check1 < 17){
        alert("message 3");
        window.location.href = 'myteam.php';}       

else {return true;}    
    }    
    window.onload = checkfs;

HTML

    <input name="countpl" id="countpl" type="hidden" value="<?php echo $row_checkfs['count(f_player.id)']; ?>"/>
<input name="rating" id="rating" type="hidden" value="<?php echo $row_checkfs['rating']; ?>"/>                                

作成されたコントロールの種類に応じて、正しいアラートを視覚化する方法がわかりません。現時点では、見つかった問題が何であれ、常に "alert( "message 1" )" が表示されます。rating3 == 3 && check3 > 22 || の場合にメッセージ 1 が表示されるようにします。check3 < 19、rating2 == 2 && check2 > 21 の場合に表示されるメッセージ 2 || check2 < 18 など。この結果を得るためにコードを変更するにはどうすればよいですか?

4

3 に答える 3

3

これを試して:

function checkfs()
{ 
var rating = (document.getElementById("rating").value);
var check = (document.getElementById("countpl").value);
alert("rating="+rating+" - Check="+check);
    if (rating == 3 && (check > 22 || check < 19)){
        alert("message 1");
        window.location.href = 'myteam.php';}

else if (rating == 2 && (check > 21 || check < 18)){
        alert("message 2");
        window.location.href = 'myteam.php';}

else if (rating == 1 && (check > 20 || check < 17)){
        alert("message 3");
        window.location.href = 'myteam.php';}       

else {return true;}    
    }    

実際の値を表示するアラートを追加しました。

また、変数を 6 つではなく 2 つ使用し、"or" 条件に括弧を追加しました。

失敗の主な理由は、「または」条件の括弧だったと思います。

演算子の優先順位に関する理論を確認する必要があります。

于 2013-10-08T13:07:43.910 に答える
1

また、反復タスクに関数を使用することを検討してください。

function checkfs()
{ 
    var rating = (document.getElementById("rating").value);
    var check = (document.getElementById("countpl").value);
    alert("rating="+rating+" - Check="+check);
    if (rating == 3 && checkThis(check,19,22)){
        alert("message 1");
        window.location.href = 'myteam.php';}

    else if (rating == 2 && checkThis(check,18,21)){
        alert("message 2");
        window.location.href = 'myteam.php';}

    else if (rating == 1 && checkThis(check,17,20)){
        alert("message 3");
        window.location.href = 'myteam.php';}       

    else {return true;}    
} 

function checkThis(tocheck, min, max)
{
   return tocheck<min || tocheck>max;
}
于 2013-10-08T13:23:32.137 に答える