admin管理员组文章数量:1389749
I have a little script that makes a multiple check on two hidden input fields:
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']; ?>"/>
I can’t understand how to visualize the correct alert depending on the type of control that has been made. At the moment I always see "alert( "message 1" )" whatever is the problem that has been found. I want the message 1 to appear if rating3 == 3 && check3 > 22 || check3 < 19, the message 2 to appear if rating2 == 2 && check2 > 21 || check2 < 18 etc. How do I modify the code in order to get this result?
I have a little script that makes a multiple check on two hidden input fields:
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']; ?>"/>
I can’t understand how to visualize the correct alert depending on the type of control that has been made. At the moment I always see "alert( "message 1" )" whatever is the problem that has been found. I want the message 1 to appear if rating3 == 3 && check3 > 22 || check3 < 19, the message 2 to appear if rating2 == 2 && check2 > 21 || check2 < 18 etc. How do I modify the code in order to get this result?
Share Improve this question asked Oct 8, 2013 at 12:56 blipsblips 411 silver badge5 bronze badges 2-
1
Your calls to
.getElementById()
... you're getting the same "id" value 3 times. Why? In any case, the return value from that call is a DOM element, and paring a DOM element to a number makes no sense. – Pointy Commented Oct 8, 2013 at 12:58 -
1
The way you're doing this,
rating1
,rating2
, andrating3
will all have the same value. Same withcheck1
,check2
, andcheck3
. – Bucket Commented Oct 8, 2013 at 13:01
3 Answers
Reset to default 3Try this:
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;}
}
I added an alert to see the actual values.
I also used 2 variables instead of 6, and added the parenthesis on the "or" condition.
I think your main reason of failure was the parenthesis on the "or" condition.
You should review the theory about the precedence of the operators.
try
<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>
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;
}
本文标签: Multiple alert JavascriptStack Overflow
版权声明:本文标题:Multiple alert Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744668475a2618677.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论