我有一个小脚本,可以对两个隐藏的输入字段进行多重检查:
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']; ?>"/>
我无法理解如何根据已经进行的控制类型可视化正确的警报。目前我总是看到“警告(”消息1“)”“无论发现什么问题。如果rating3 == 3&amp;&amp ;;我希望消息1出现check3&gt; 22 || check3&lt; 19,如果rating2 == 2&amp;&amp ;;则出现消息2 check2&gt; 21 || check2&lt; 18等 如何修改代码才能获得此结果?
答案 0 :(得分: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个,并在“或”条件中添加了括号。
我认为失败的主要原因是“或”条件下的括号。
您应该查看有关运营商优先权的理论。
答案 1 :(得分: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>
答案 2 :(得分: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;
}