简体   繁体   中英

HTML - JavaScript onclick() function not working

I have JavaScript code including several functions, the main one being CheckForm() .

The function is called by clicking the 'Submit' button:

<td><input type="submit" name="submit1" id="submit1" value="Register" onclick="return CheckForm();"/></td>    

But when the button is pressed nothing happens (the function isn't performed). What can I do to fix this?

Have you tried debugging your code using FireBug or jsFiddle ?

Some possible causes are an incorrectly named function or function call(remember that JavaScript is case sensitive), an error in your function or your JavaScript code not being referenced in your page.

If you aren't using either of the above tools then try using a console.log or alert inside your function to see if it is being called.

You can use it like this.

function CheckForm()
{
 //doing stuff
  return true;
}

<form id="formToCheck"></form>


$('#formToCheck').submit(CheckForm);

Hope it helps

if you want to do it without jquery just add on

   <form onSubmit="return CheckForm()"></form>

You can read more about form validation without jQuery here -> http://www.w3schools.com/js/js_form_validation.asp#gsc.tab=0

Maybe if it doesn't work you have some errors. Check JS console in your browser and remove them.

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