Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未定义onsubmit函数_Javascript_Jquery_Forms_Onsubmit - Fatal编程技术网

Javascript 未定义onsubmit函数

Javascript 未定义onsubmit函数,javascript,jquery,forms,onsubmit,Javascript,Jquery,Forms,Onsubmit,为什么它说我没有定义我的功能?是否因为我已将我的函数放置在文档就绪函数中也许我必须提到,如果我的陈述不正确,我想使用这个函数来阻止提交 我的html表单标记: <form class="text-left" method="post" action="" onsubmit="return myFunction();"> 这是因为myFunction是在$(document.ready的范围内定义的,并且在外部不可见。在外部定义它及其从属变量 //the number generat

为什么它说我没有定义我的功能?是否因为我已将我的函数放置在文档就绪函数中也许我必须提到,如果我的陈述不正确,我想使用这个函数来阻止提交

我的html表单标记:

<form class="text-left" method="post" action="" onsubmit="return myFunction();">

这是因为
myFunction
是在
$(document.ready
的范围内定义的,并且在外部不可见。在外部定义它及其从属变量

//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;
}
更新

删除表单的内联
onsbuit
,并使用
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;       
    });  
});

尝试在此处使用
窗口
对象:

$( 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
语句)的作用域是它们在其中声明的函数


如果要将
myFunction
用作全局函数,请将其及其所依赖的变量从中声明的匿名函数中移出


或者,您可以显式创建引用它的全局文件:

window.myFunction = myFunction

然而,最好的解决方案是,首先不要使用全局变量

删除
onsubmit
属性,改为使用JavaScript绑定事件处理程序

$('form').on('submit', myFunction);
确保捕获事件对象:

function myFunction(e){
如果您不希望表单提交,请防止默认表单提交行为:

$("#captchaInput").css("border-color", "red");
e.preventDefault();

为什么要混合使用javascript和jquery?使用全局对象来声明它!只需添加
window.myFunction=function(){
而不是
function myFunction(){
它应该会起作用!@Mivaweb这是一个非常糟糕的表达方式。jQuery是JavaScript,它们共存,应该是“混合的”。更好的问题是“为什么要将内联JavaScript与非全局范围的事件处理程序混合?”这不会起作用,因为
myFunction()
仍在尝试使用
sum
。非常感谢您的帮助,它成功了!@MartinNielsen,很高兴它成功了。但我建议您按照Quentin的回答,并在('submit')上绑定jquery
instead@MartinNielsen,如果您不知道如何操作,请告诉我或昆廷,以便我们提供更多详细信息
function myFunction(e){
$("#captchaInput").css("border-color", "red");
e.preventDefault();