javascript警报具有不同的价值

时间:2014-05-07 06:58:33

标签: javascript html

我有一个活跃和离线值的链接..

while($s = mysql_fetch_array($strength))
{
?>
<a href="updateperiode.php?id=<?php echo $s['periode_id']; ?>" onclick="return confirmReset()">
<?php if($s['status'] == 'Active')
{
?>
<span class="badge_style b_online">Active</span>
<?php
}
else
{
?>
<span class="badge_style b_offline">Offline</span>
<?php
}
?>
</a>
<?php } ?>

我想要做的是,如果值为Active,并且用户单击它,则弹出警报..并表示您要取消激活,如果值处于脱机状态,则当用户单击时,警报会说你要激活..

我的javascript就是这样..

<script>
    function confirmReset() {
   var div = document.getElementById("div");
   var span = div.getElementsByTagName("span");

   if(span.innerHTML == 'Active')
   {
        var question = confirm("Are you sure want to deactive?");
   } 
   else 
   {
        var question = confirm("Are you sure want to activate?");
   }

    if(question){

    return true;


    }else{


    return false;

    }
}
</script>

即使值处于活动状态,输出始终为Are you sure want to activate?

这是我的完整代码..

http://pastebin.com/FteRz6VN

这是屏幕..

http://puu.sh/8CyBy.jpg

3 个答案:

答案 0 :(得分:3)

请试试这个

 <?php
    $i = 0;
    while($s = mysql_fetch_array($strength))
    {
    ?>
    <a href="updateperiode.php?id=<?php echo $s['periode_id']; ?>" onclick="return confirmReset('<?php echo 'status'.$i ?>')">
    <?php if($s['status'] == 'Active')
    {
    ?>
    <span class="badge_style b_online"  id="status<?php echo $i ?>">Active</span>
    <?php
    }
    else
    {
    ?>
    <span class="badge_style b_offline"  id="status<?php echo $i ?>">Offline</span>

    }
    ?>
    </a>
    <?php $i++; } ?>
    <script>
     function confirmReset(id) {
       var statusDiv = document.getElementById(id).innerHTML;
       if(statusDiv == "Active"){
            var question = confirm("Are you sure want to deactive?");
       } else {
            var question = confirm("Are you sure want to activate?");
       }
       if(question){
            return true;
        } else {
            return false;
        }
    }
    </script>

答案 1 :(得分:0)

更改span变量:

var span = div.getElementsByTagName("span")[0];

答案 2 :(得分:0)

您可以使用jquery轻松解决此问题,代码如下:

$(document).ready(function() {
$(".badge_style").click(function(){
var isActive = $(this).html();
if(isActive == "Active"){
...Do what you want to do if active
alert("do you want to activate!");
}else{
... else part
}

});

});

无需调用confirmReset函数