按钮,超时,警报未捕获TypeError:

时间:2015-11-28 20:58:36

标签: javascript html getelementbyid uncaught-typeerror

我有2个按钮,我想点击" Button1"第一次和5秒后。 "将Button2" 再过10秒重定向到另一个网址。

<?php 
    if(!isset($_SESSION)){
        session_start();
    }

    $student1 = array(
        array(
            'Name'=>$_POST['name'],
            'Matric-No'=>$_POST['matric'],
            'Gender'=>$_POST['gender'],
            'Day'=>$_POST['DOBDay'],
            'Month'=>$_POST['DOBMonth'],
            'Year'=>$_POST['DOBYear'],
            'Citizen'=>$_POST['citizen'],
            'Marital'=>$_POST['kahwin'],
            'Religion'=>$_POST['religion'],
            'Active'=>$_POST['active'],
            'Year-of-Study'=>$_POST['Cyear'],
            'ID-Number'=>$_POST['idno'],
            'Email'=>$_POST['email']
        )
    );

    $_SESSION['data'] = $student1;

    print_r($_SESSION);

?>

我得到了

  

未捕获的TypeError:无法读取属性&#39;点击&#39; null。

按钮工作并单击,但重定向的代码不是。奇怪的是我使用警报来检查代码,它首先运行警报,然后点击&#34; Button1&#34;之后&#34; Button2&#34;然后我得到错误并且它不会继续执行最后一个代码。我不知道该怎么办..

1 个答案:

答案 0 :(得分:1)

我认为你的第二个警告声明是错误的。 试试这个:

document.getElementById('Button1').click();
alert("After clicking Button1");

// wait 5 seconds
setTimeout(function(){
    document.getElementById('Button2').click();
    alert("After clicking Button2");

    // wait 10 seconds
    setTimeout(function(){
        window.location.href="http://google.com";
    }, 10000);
}, 5000);

确保Button2存在,因为您获得的错误表明它没有。 您的HTML应如下所示:

<button id="Button1">this is button 1</button>
<button id="Button2">this is button 2</button>