gpt4 book ai didi

javascript - 多个警报 JavaScript

转载 作者:行者123 更新时间:2023-11-28 13:39:04 25 4
gpt4 key购买 nike

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

    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 等如何修改代码才能得到这个结果?

最佳答案

试试这个:

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 个,并在“或”条件上添加了括号。

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

您应该回顾一下有关运算符优先级的理论。

关于javascript - 多个警报 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19248337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com