简体   繁体   中英

HTML5 not calling javascript function

For some reason that I cannot fathom, the following code does not work:

<!DOCTYPE html>
<html>
<head>
<title>Devit Ajax Test</title>
<script language="JavaScript" type="text/javascript" src="js/voucher.js"></script>
</head>
<body onLoad="document.VU.cellNo.focus()">
<form name="VU" id="VU" method="post" >
    <div class="FormItem">
        <label>Cell Number:</label>
        <input type="tel" id="cellNo" name="cellNo" required > 
    </div>
    <p>
    <div class="FormItem">
        <label>PIN:</label>
        <input id="pin" name="pin" type="text" required >
    </div>
    <div class="FormItem">
        <p> 
        <input type="submit" value="Submit" onclick="submit_click()"> 
        </p>  
    </div>
<center>
<div id="loadingNode"></div>
</center>
</body>
</html>
<script>
function submit_click()
{
    alert("progExe()");
    progExe("http://web.devit.co.za/PEMObi/Pages/HomePage/Homepage.aspx?CellNumber="+VU.cellNo.value"&PIN="+VU.pin.value);    
}
</script>

The submit_click() function never gets called despite the onclick call.

This issue is with the line that calls progExe() -- the string you're building in the function call is missing a plus sign between to parts.

This missing plus sign is a syntax error, and makes it invalid Javascript code. This in turn means that the entire code block is made invalid, which means that the browser won't execute it at all.

To prove this, we can comment out the progExe() line. This will remove the syntax error, and thus the rest of the code will be seen as valid and therefore you will find that the alert() starts working.

Fixing the error of course will also resolve the problem. Add a plus sign after VU.cellNo.value .

It's obvious once you see it, but it is the kind of bug that is easy to miss, and it demonstrates is why it is a good idea to write code using a decent editor. If you open the code in a text editor or IDE with good syntax highlighting features, it will show you exactly where the problem is. Netbeans, for example, will flag the line with an error message. It makes it much easier to find these things.

You are missing a + in your string in progExe.

progExe(" http://web.devit.co.za/PEMObi/Pages/HomePage/Homepage.aspx?CellNumber= "+VU.cellNo.value + "&PIN="+VU.pin.value);

You're missing a + in your code:

Change

progExe("http://web.devit.co.za/PEMObi/Pages/HomePage/Homepage.aspx?CellNumber="+VU.cellNo.value"&PIN="+VU.pin.value);  

to

progExe("http://web.devit.co.za/PEMObi/Pages/HomePage/Homepage.aspx?CellNumber="+VU.cellNo.value+"&PIN="+VU.pin.value);  

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