在onsubmit上加载多个Javascript文件

时间:2014-01-12 14:26:50

标签: javascript file function call

我已经定义了3个带有多个函数的javascript文件,并且我在每个函数中都有事件处理程序onsubmit(基本上是相同的结构)问题是,当我将这3个文件作为文件路径插入到body部分的底部时,只有2个他们是装,而第三个不是,我不明白为什么...... 这是代码:

function init() {
    'use strict';

    // Listen to see whether the form has been submitted:
    //get a handle to my form
    var demogrForm = document.getElementById("Demographics");   
    demogrForm.onsubmit = validateInputs;

    console.log("submit");
} // End of init() function.

window.onload = init;
console.log("init");
基本上,每个“onsubmit”调用的函数在3个文件中是不同的,但处理调用的代码是相同的......你有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试使用jQuery $(function(){});或者更改你的window.onload。它应该是这样的:

window.onload=function(){
    console.log("init");
    // Listen to see whether the form has been submitted:
    //get a handle to my form
    var demogrForm = document.getElementById("Demographics");   
    demogrForm.onsubmit = validateInputs;

    console.log("submit");
};

或使用jQuery:

$(function(){
    console.log("init");
    // Listen to see whether the form has been submitted:
    //get a handle to my form
    var demogrForm = document.getElementById("Demographics");   
    demogrForm.onsubmit = validateInputs;

    console.log("submit");
});