HTML - JavaScript onclick() 函数不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15762056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML - JavaScript onclick() function not working
提问by user2235692
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?
但是当按下按钮时什么也没有发生(不执行该功能)。我能做些什么来解决这个问题?
回答by AGB
Have you tried debugging your code using FireBugor jsFiddle?
您是否尝试过使用FireBug或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.
一些可能的原因是错误命名的函数或函数调用(请记住 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 来查看它是否被调用。
回答by Robert
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
您可以在此处阅读有关不使用 jQuery 的表单验证的更多信息 -> 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 控制台并删除它们。