0

我有一个小脚本,可以对两个隐藏的输入字段进行多次检查:

    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;}    
    }    

我添加了一个警报以查看实际值。

我还使用了 2 个变量而不是 6 个变量,并在“或”条件上添加了括号。

我认为您失败的主要原因是“或”条件的括号。

您应该复习有关运算符优先级的理论。

于 2013-10-08T13:07:43.910 回答
1

尝试

<script>
function checkfs()
{ 
var rating = (document.getElementById("rating").value);
var check = (document.getElementById("countpl").value);

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;}    
}    
window.onload = checkfs;
</script>
于 2013-10-08T13:13:26.580 回答
1

Also consider using functions for repetitive tasks,

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 回答