[英]onsubmit function is not defined
Why is it saying that I have not defined my function? 为什么说我没有定义我的功能? Is it because I have placed my function inside a document ready function? 是因为我已将函数放置在文档就绪函数中吗? - Maybe I have to mention that I want to use this function to prevent submitting if my statement is not true. -也许我不得不提一下,如果我的陈述不正确,我想使用此功能阻止提交。
my form tag in html: 我在html中的表单标签:
<form class="text-left" method="post" action="" onsubmit="return myFunction();">
below is script(this script is in my head section) : 下面是脚本(此脚本在我的头部):
$( document ).ready(function() {
//the number generators and sum of the two numbers
var numberOne = Math.floor((Math.random() * 10) + 1);
var numberTwo = Math.floor((Math.random() * 10) + 1);
var sum = numberOne + numberTwo;
//write the math question to the div
document.getElementById("captchaOutput").innerHTML = numberOne+ " og " +numberTwo;
//alert(sum);
function myFunction(){
var humanInput = $('#captchaInput').val();
if(humanInput == sum){
$("#captcha_service_err").css("display", "inline")
$("#captchaInput").css("border-color", "red");
return false;
}
$("#captcha_service_err").css("display", "none")
$("#captchaInput").css("border-color", "");
return true;
}
});
it because i have placed my function inside a document ready function? 这是因为我已将我的函数放置在文档就绪函数中?
Yes. 是。 Function declarations are (like var
statements) scoped to the function they are declared within. 函数声明(如var
语句)的作用域仅限于在其中声明的函数。
If you want to use myFunction
as a global then move it, and the variables it depends on, out of the anonymous function you declare it with in. 如果要将myFunction
用作全局变量,则将其及其依赖的变量从使用in声明的匿名函数中移出。
Alternatively, you could explicitly create a global that references it: 另外,您可以显式创建一个引用它的全局变量:
window.myFunction = myFunction
The best solution, however, is to not use a global in the first place. 但是,最好的解决方案是首先不要使用全局变量。
Remove the onsubmit
attribute and bind your event handlers with JavaScript instead. 删除onsubmit
属性,并使用JavaScript绑定事件处理程序。
$('form').on('submit', myFunction);
Make sure you capture the event object: 确保捕获事件对象:
function myFunction(e){
Then prevent the default form submission behaviour if you don't want it to submit: 然后,如果您不希望默认表单提交行为,则阻止它:
$("#captchaInput").css("border-color", "red");
e.preventDefault();
This is because myFunction
is defined within the scope of the $(document).ready
and is not visible outside. 这是因为myFunction
是在$(document).ready
范围内定义的,并且在外部不可见 。 Define it outside along with its dependant variables 在外部定义它及其因变量
//the number generators and sum of the two numbers
var numberOne = Math.floor((Math.random() * 10) + 1);
var numberTwo = Math.floor((Math.random() * 10) + 1);
var sum = numberOne + numberTwo;
$(document).ready(function() {
//write the math question to the div
document.getElementById("captchaOutput").innerHTML = numberOne + " og " + numberTwo;
//alert(sum);
});
function myFunction() {
var humanInput = $('#captchaInput').val();
if (humanInput == sum) {
$("#captcha_service_err").css("display", "inline")
$("#captchaInput").css("border-color", "red");
return false;
}
$("#captcha_service_err").css("display", "none")
$("#captchaInput").css("border-color", "");
return true;
}
Update 更新资料
Remove the inline onsbumit
for the form and use on()
like below 删除表单的内联onsbumit
并使用on()
如下所示
$( document ).ready(function() {
//the number generators and sum of the two numbers
var numberOne = Math.floor((Math.random() * 10) + 1);
var numberTwo = Math.floor((Math.random() * 10) + 1);
var sum = numberOne + numberTwo;
//write the math question to the div
document.getElementById("captchaOutput").innerHTML = numberOne+ " og " +numberTwo;
//alert(sum);
$('form').on('submit', function(){
var humanInput = $('#captchaInput').val();
if(humanInput == sum){
$("#captcha_service_err").css("display", "inline")
$("#captchaInput").css("border-color", "red");
return false;
}
$("#captcha_service_err").css("display", "none")
$("#captchaInput").css("border-color", "");
return true;
});
});
Try using window
Object here: 尝试在此处使用window
对象:
$( document ).ready(function() {
//the number generators and sum of the two numbers
var numberOne = Math.floor((Math.random() * 10) + 1);
var numberTwo = Math.floor((Math.random() * 10) + 1);
var sum = numberOne + numberTwo;
//write the math question to the div
document.getElementById("captchaOutput").innerHTML = numberOne+ " og " +numberTwo;
//alert(sum);
window.myFunction = function () {
var humanInput = $('#captchaInput').val();
if(humanInput == sum){
$("#captcha_service_err").css("display", "inline")
$("#captchaInput").css("border-color", "red");
return false;
}
$("#captcha_service_err").css("display", "none")
$("#captchaInput").css("border-color", "");
return true;
}
});
var sum = 0;
$( document ).ready(function() {
//the number generators and sum of the two numbers
var numberOne = Math.floor((Math.random() * 10) + 1);
var numberTwo = Math.floor((Math.random() * 10) + 1);
sum = numberOne + numberTwo;
//write the math question to the div
document.getElementById("captchaOutput").innerHTML = numberOne+ " og " +numberTwo;
//alert(sum);
});
function myFunction(){
var humanInput = $('#captchaInput').val();
if(humanInput == sum){
$("#captcha_service_err").css("display", "inline")
$("#captchaInput").css("border-color", "red");
return false;
}
$("#captcha_service_err").css("display", "none")
$("#captchaInput").css("border-color", "");
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.