简体   繁体   中英

unable to call javascript function using button's onclick event

My display.php file has the code for fetching data from database and display it in html table. I want to display this table on my index page only in the <div> displayTable section and for that I've called the showTbale() using onclick of my button abc . But the problem is that the function showTable() doesn't get called even though I've successfully called the logoutEmp() using the onclick of the logoutButton . I don't understand what's the problem. Any help would be appreciated.

NOTE: I tried using form action to call display.php but it displays the table in another white page which I don't want. Also I don't want to include display.php code in my index page.

say this is my index file :

<head>
<meta http-eqiv="X-UA-Compatible" content="IE=9,chrome=1">
<link rel="stylesheet" type="text/css" href="page_style.css">
<script src="jquery-1.10.1.js"></script>
</head>

<body>
<script>

function logoutEmp()
{
  window.location="saveLogout.php";
}   

function showTable()
{
  var month= $('#mnth: selected').val();
  alert (month);

  $("#displayTable").load("display.php",{ mnth: month});
}   

</script>

<h1>Employee Account</h1>

<div id="logoutEmp">
<button type="button" id="logoutButton" onclick="logoutEmp()">Logout</button>
</div>

<hr>

<div id="shw">
<select name="mnth">
<option value="Jan">January</option>
<option value="Feb">February</option>
<option value="Mar">March</option>
<option value="Apr">April</option>
<option value="May">May</option>
<option value="Jun">June</option>
<option value="Jul">July</option>
<option value="Aug">August</option>
<option value="Sep">September</option>
<option value="Oct">October</option>
<option value="Nov">November</option>
<option value="Dec">December</option>
</select>
<button type="button" id="abc" onclick="showTable()">Display</button> 


<div id="displayTable">
</div>

</div>

</body>
</html>

change your code like this:

function logoutEmp(){
 window.location.href="saveLogout.php";
 } 

You had an extra slash, and a syntax error.

Also, you should change showTable that was wrong as well :

function showTable()
{
  var month= $('#mnth').val();
  alert (month);

 $("#displayTable").load("display.php",{ mnth: month});
 }   

And add your id to your select:

<select name="mnth" id="mnth">

Here is the demo my friend.

I guess:

function logoutEmp()
{
 window.location="saveLogout.php";
 \} 

that backslash should not be there:

function logoutEmp()
{
 window.location="saveLogout.php";
}

you can not call showTable() because in JavaScript if there is any error script after that error would not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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