简体   繁体   中英

HTML hyperlink not calling JavaScript function

Can someone help me with this problem: I have a table in a jsp page, with the text in one column being hyperlinks. Whenever anyone of these hyperlinks is clicked the whole table should refresh and repopulate based on the value of the hyperlink clicked. My problem is currently when the hyperlink is clicked the page refreshes with an empty table. I have the following line of HTML code for performing this in my jsp page:

<TD><A href="http://localhost:8080/pmweb/gui.jsp" onclick="getResults(param)">hyperlinktext</A></TD>;

Below is my getResults function in javascript in the same JSP page:

<script type="text/javascript">
var httpRequest; 

function getResults(param) {

    var url = "http://localhost:8080/pmweb/api/GetResultsByParam?param=" + param;

    httpRequest = new XMLHttpRequest();  

    httpRequest.open("GET", url, true);

    httpRequest.onreadystatechange = function() {processRequest(); } ;

    httpRequest.send(null);   
}

I have verified that the getResults function above is working fine itself. When I debugged it I noticed that this getResults function is not entered when the hyperlink is clicked. Anyone know how to get the hyperlink calling the javascript function properly? Thanks very much in advance!

Clicking on an anchor will take the user to the page specified in its href attribute. You must return false from the click handler to prevent this.

hyperlinktext

And return false from the getResults method.

Also, as raj noted, make sure that param is defined.

When you call:

...
<TD><A href="http://localhost:8080/pmweb/gui.jsp"
    onclick="getResults(param)">hyperlinktext</A></TD>;

'param' is undeclared in 'getResults(param)'. Pass some value instead:

...
<TD><A href="http://localhost:8080/pmweb/gui.jsp"
    onclick="getResults('name')">hyperlinktext</A></TD>;

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