繁体   English   中英

onClick javascript功能不起作用

[英]onClick javascript function is not working

继承人的基本思想:我有一个html页面,其中有一个表单来做一些事情和一个相应的javascript文件。 我无法使用php,jquery或JSON进行此项目。

在javascript中,我有与表单提交按钮连接的get_student_info()函数。 但是,每当我点击“提交”按钮而不是将我带到该功能(打印警报和其他所有内容)时,我都跳到了第二页。

该代码段如下所示:

<!DOCTYPE html>
<html>
<head>
     <script type = "text/javascript" src = "app.js" defer></script>
</head>
<body>
<a name = "page7"></a>
<div id="page7">
<header id="page7_header">
      <h3 id="page7_h3"> SSC Information </h3>
</header>


<div id = "page7_empty_space"></div>

<center id="page7_ssc_info" >      

      <form name="page7_ssc_input" id="page7_table" method="post">

        <label id="page7_passing_year_p">Passing Year : 
        <label>

        <select id="page7_ssc_year_dropDown" name="ssc_passing year">
                      <option value="sscyear1">2009</option>
                      <option value="sscyear2">2010</option>
                      <option value="sscyear3">2011</option>
                      <option value="sscyear4">2012</option>
                      <option value="sscyear5">2013</option>
                      <option value="sscyear6">2014</option>
           </select>
           <label id="page7_board_p">
          Board : 
          </label>

          <select id="page7_ssc_board_dropDown" name="ssc_board">
                      <option value="sscboard1">Dhaka</option>
                      <option value="sscboard2">Chittagong</option>
                      <option value="sscboard3">Barisal</option>
                      <option value="sscboard4">Comilla</option>
                      <option value="sscboard5">Sylhet</option>
                      <option value="sscboard6">Dhaka</option>
                      <option value="sscboard7">Chittagong</option>
                      <option value="sscboard8">Barisal</option>
                      <option value="sscboard9">Comilla</option>
                      <option value="sscboard10">Sylhet</option>
                  </select>
            <label id="page7_gpa"> GPA:
            </label>

            <input type="text" value="" palceholder="GPA" id="page7_input_gpa">

            <label id="page7_group">Group :
            </label>

            <input type="text" value="" palceholder="group" id="page7_ssc_group">

    <button  id="page7_button_next" onclick = get_student_info("page7_ssc_year_dropDown", "page7_ssc_board_dropDown", "page7_input_gpa","page7_ssc_group", "page8");>
       Next
    </button> 

    </form>
    <!-- Database sending data part -->

</center>

<footer id="page7_footer">
</footer>
</div>
</body>
</html>    

这是Javascript部分:

window.addEventListener = disableFirstPage();

// global variable zone
var timeout;


// global variable zone

function disableFirstPage()  
{
   alert("method: disableFirstPage()");    
   // timeout is not working properly

   timeout = setTimeout(PageJumping("page2"),5000); //Jump to Page 2

   document.getElementById("page1").style.visibility = "hidden";
   document.getElementById("page2").style.visibility = "visible";
   //disableScrolling();
   disableTimeOut(timeout);
}

function PageJumping(x)
{  
   alert("Jumping to "+x);
   window.location.hash = x; 
}

function disableScrolling()
{  
    alert("disabling scrolling");
    document.body.style.overflow = "hidden";
}

function disableTimeOut(timeout)
{
   alert("timeout is disabled.")
   clearTimeout(timeout);
}   


function get_student_info(passingYear, board, gpa,group, nextPage)
  // this method has a page jumping after confirming student information is okay and valid;
{
   alert("am i here");
   var passingYear = document.getElementById(passingYear).value;
   var board = document.getElementById(board).value;
   var gpa = document.getElementById(gpa).value;
   var group = document.getElementById(group).value;

   alert("confirm your information using ok/cancel box");
   alert("passingYear: "+passingYear+" board: "+board+ " gpa: "+gpa+" group: "+group);
   PageJumping(nextPage);
}   

提前致谢。

您需要将onclick属性值用引号引起来:

... onclick='get_student_info("page7_ssc_year_dropDown", "page7_ssc_board_dropDown", "page7_input_gpa","page7_ssc_group", "page8");' ...

为了防止表单提交,您需要从onsubmit事件中返回false 例如,在您的情况下,

<form name="page7_ssc_input" id="page7_table" method="post" onsubmit="return false;">
...
</form>

同样,在onsubmit的事件处理程序中执行此操作在语义上更正确,例如

<form name="page7_ssc_input" id="page7_table" method="post" onsubmit='get_student_info("page7_ssc_year_dropDown", "page7_ssc_board_dropDown", "page7_input_gpa","page7_ssc_group", "page8"); return false;'>
...
    <button type="submit" id="page7_button_next">
       Next
    </button>
</form>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM