通过使用警报确定按钮进行重定向不起作用

时间:2019-07-08 01:43:29

标签: javascript

我在网上找到了有关计时器的教程,并尝试对其进行修改。我在警告原因之后添加了window.location.href = "typage.php";,因为我已阅读到单击警告按钮上的“确定”按钮后,它可用于重定向到页面。

问题是单击“确定”按钮后,它没有显示应该重定向的页面。相反,它仍然显示倒数计时器并显示“未找到记录”。这是不正确的,因为我需要显示typage.php。

我还可以将时间格式更改为mm:ss格式吗?

Error

<?php include ("inc/userheader.php");?>

<head>
    <meta charset="utf-8">
    <title>Quiz</title> 
<script> 
        var mins = 1; 

        var secs = mins * 60; 

        function countdown() { 
            setTimeout('Decrement()', 60); 
        } 

        function Decrement() { 
            if (document.getElementById) { 
                minutes = document.getElementById("minutes"); 
                seconds = document.getElementById("seconds"); 


                if (seconds < 59) { 
                    seconds.value = secs; 
                } 

                else { 
                    minutes.value = getminutes(); 
                    seconds.value = getseconds(); 
                } 

                if (mins < 1) { 
                    minutes.style.color = "red"; 
                    seconds.style.color = "red"; 
                } 

                if (mins < 0) { 
                    alert("Time's up!"); 
                    window.location.href = "typage.php";

                    minutes.value = 0; 
                    seconds.value = 0; 
                } 
                else { 
                    secs--; 
                    setTimeout('Decrement()', 1000); 
                } 
            } 
        } 

        function getminutes() { 
            mins = Math.floor(secs / 60); 
            return mins; 
        } 

        function getseconds() { 

            return secs - Math.round(mins * 60); 
        }

</script>

<body onload="countdown();"> 

<div class="container">
    <h1> Quiz</h1>


    <?php echo form_open('users/redirect/', ['class' => 'form-horizontal']);?> 
   <!-- onload function is evoke when page is load --> 
<!--countdown function is called when page is loaded --> 


    <div> 
        <b>Time Left :
        <input id="minutes" type="text" style="width: 10px; 
             border: none; font-size: 16px;  
            font-weight: bold; color: black;"><font size="5"> : 
                        </font> 
        <input id="seconds" type="text" style="width: 20px; 
                        border: none; font-size: 16px; 
                        font-weight: bold; color: black;"></b>
    </div> 

    <br>

    <?php if(count($questions) > 0):?>

        <?php $index = 1 ?>
        <?php foreach($questions as $row):?>
        <br>
        <div class="row justify-content-center view">
        <div class="col-lg-10">

        <p><?=$index++?>. <b><?=$row->ques?></b></p>

        <?php 
                $choices= array($row->ch_des1, $row->ch_des2, $row->ch_des3, $row->ch_des4);
                shuffle($choices);
            ?>

            <?php foreach($choices as $ch_des):?>
                <input type="radio" name="ch_id<?=$row->ch_id?>" value="<?=$ch_des?>"> <?=$ch_des?><br>
            <?php endforeach;?>

        </div>
        </div>
        <?php endforeach;?>
        <?php else:?>
            <tr>
                <td>No records found!</td>
            </tr>
    <?php endif;?>

        </div>
    </div>

        <br><br>
        <center><button type ="submit" class="btn btn-primary "> Submit </button></center>

    </form>


</div>
</body>


<?php include ("inc/footer.php");?>

2 个答案:

答案 0 :(得分:0)

此条件if (document.getElementById) {确实没有必要。

var mins = 1;
var minutes = document.getElementById("minutes");
var seconds = document.getElementById("seconds");

var secs = mins * 60;

function countdown() {
  setTimeout('Decrement()', 60);
}

function Decrement() {
  if (seconds < 59) {
    seconds.value = secs;
  } else {
    minutes.value = getminutes();
    seconds.value = getseconds();
  }

  if (mins < 1) {
    minutes.style.color = "red";
    seconds.style.color = "red";
  }

  if (mins < 0) {
    alert("Time's up!");
    window.location.href = "typage.php";

    minutes.value = 0;
    seconds.value = 0;
  } else {
    secs--;
    setTimeout('Decrement()', 1000);
  }
}

function getminutes() {
  mins = Math.floor(secs / 60);
  return mins;
}

function getseconds() {
  return secs - Math.round(mins * 60);
}
<body onload="countdown();">
  <input type="text" id="minutes" />
  <input type="text" id="seconds" />
</body>

答案 1 :(得分:-1)

您可以编写一个仅包含php的简单window.location.href = "typage.php"文件,并测试该php文件是否可以redirect到您想要的页面。