JS如果条件警报

时间:2014-01-23 03:18:28

标签: if-statement conditional-statements alerts

我只有php函数,我想使用JS警告框返回警报消息。

当我执行第一个if条件时,警报工作正常。但是一旦我添加其他警报框就不起作用(== 2和3)。

任何想法我做错了什么?

谢谢!

$(document).ready( function() {

if (<?php echo error_for('fileTmpLoc') ?>==1)
{
alert("Error: No image was selected.");
}
if (<?php echo error_for('fileTmpLoc') ?>==2)
{
alert("Error: Your file was too large. It was larger than 3.5 Megabytes in size.");
}
if (<?php echo error_for('fileTmpLoc') ?>==3)
{
alert("Error: Your file is too small. It was smaller than 200 KB in size.");
}

});

2 个答案:

答案 0 :(得分:0)

我没有对此进行测试,但您是否尝试过使用if else语句?

$(document).ready( function() {

if (<?php echo error_for('fileTmpLoc') ?>==1)
{
   alert("Error: No image was selected.");
}
else if (<?php echo error_for('fileTmpLoc') ?>==2)
{
   alert("Error: Your file was too large. It was larger than 3.5 Megabytes in size.");
}
else if (<?php echo error_for('fileTmpLoc') ?>==3)
{
   alert("Error: Your file is too small. It was smaller than 200 KB in size.");
}

});

答案 1 :(得分:0)

您还可以通过PHP处理错误条件检查和JS警报输出,例如:

$(document).ready( function() {
<?php 
    switch(error_for('fileTmpLoc')) {
        case 1 : echo 'alert("Error: No image was selected.");';
                 break;
        case 2 : echo 'alert("Error: Your file was too large. It was larger than 3.5 Megabytes in size.");';
                 break;
        case 3 : echo 'alert("Error: Your file is too small. It was smaller than 200 KB in size.");';
                 break;
    }
?>
});