简体   繁体   English

HTML - JavaScript onclick()函数不起作用

[英]HTML - JavaScript onclick() function not working

I have JavaScript code including several functions, the main one being CheckForm() . 我有JavaScript代码,包括几个函数,主要是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 ? 您是否尝试使用FireBugjsFiddle调试代码?

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. 一些可能的原因是命名错误的函数或函数调用(请记住,JavaScript区分大小写),函数中的错误或页面中未引用的JavaScript代码。

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. 如果您没有使用上述任何一种工具,请尝试在函数内部使用console.log或alert来查看是否正在调用它。

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 如果你想在没有jquery的情况下继续添加

   <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 你可以在这里阅读更多关于表单验证的信息 - > 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. 在浏览器中检查JS控制台并将其删除。

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

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