getElementById后为null? [英] Is null after getElementById?

查看:159
本文介绍了getElementById后为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常简单的错误在这里,我确定,但基本上我有一个表单id = code> formID 和im调用它像这样:

Very simple error here, I'm sure, but basically I have a form with id formID and im calling it like so:

<script type="text/javascript">
    function redvalidate() {
        var form = document.getElementById("formID")
        var fields = form.getElementsByClassName("validate"),

我收到错误: form is null

任何人都可以发现错误?

Can anyone spot the error?

(PS将其提交给表单)

(PS calling it onsubmit of a form)

更新
好​​的,所以基本上我有两个onsubmit一个窗体,这显然没有工作。所以我正在做的是从另一个...中调用这个函数...

UPDATE Ok so basically i have two onsubmit for one form which oviously doesnt work. So what I am doing is calling this function from within another...

表单标签:

<form name="purchaseform" id="formID" onSubmit="RememberFormFields('purchaseform','name,email,ship_to,phone_extension,pi_name');" action="process.php" method="post" enctype="multipart/form-data">

和他的RememberFormFields:

And heres RememberFormFields:

    <script type="text/javascript">
function RememberFormFields(form,list)
{
redvalidate()

...etc... rememberformfields function ...et.c..


推荐答案

假设你有html 在您的页面上的某个地方,您只需要延迟JavaScript的加载,直到DOM准备就绪后才能形成id =formID... 。您正在尝试从尚未应用于DOM的元素获取ID,因此JavaScript将其视为NULL。

Assuming you have the html <form id="formID"... somewhere on your page, you simply need to defer the loading of your javascript until after the DOM is ready. You're trying to get the ID from an element that hasn't been applied to the DOM yet and therefore JavaScript sees it as NULL.

有很多方法可以实现这一点。您可以将所有的JS放在< / body> 标签附近,您可以使用 Google推荐的延迟JavaScript加载实践,将所有JS附加到< head>

There are many ways to achieve this. You can put all your JS near the </body> tag, you can use Google's suggested deferred javascript loading practice which appends all your JS to the <head> from the bottom of the body.

这是JQuery非常方便的原因之一,因为您可以将代码封装在$(code> $(document))中。 ready(function(){}); block,所以任何DOM操作脚本将始终工作,因为它们等待,直到DOM被创建之前才被触发。它通过在现代浏览器中查找DOMContentLoaded事件来实现这一点,并为IE尝试一次又一次地滚动文本的 doScrollCheck()函数 - 如果它是可滚动的他们知道这很好。如果您不想为此加载JQuery,您可以轻松地复制此练习。

This is one reason JQuery is so handy, because you can wrap your code in a $(document).ready(function(){}); block so any DOM-manipulating scripts will always work because they wait until the DOM is created before they fire. It achieves this by looking for the "DOMContentLoaded" event in modern browsers and has a doScrollCheck() function for IE which tries to scroll the body over and over - if it is scrollable, they know it's good to go. You can easily replicate this practice if you don't want to load JQuery just for this.

JQuery的DOM准备检查:

JQuery's DOM ready check:

if ( document.addEventListener ) {
    DOMContentLoaded = function() {
        document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
        jQuery.ready();
    };

} else if ( document.attachEvent ) {
    DOMContentLoaded = function() {
        // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
        if ( document.readyState === "complete" ) {
            document.detachEvent( "onreadystatechange", DOMContentLoaded );
            jQuery.ready();
        }
    };
}

// The DOM ready check for Internet Explorer
function doScrollCheck() {
    if ( jQuery.isReady ) {
        return;
    }

    try {
        // If IE is used, use the trick by Diego Perini
        // http://javascript.nwbox.com/IEContentLoaded/
        document.documentElement.doScroll("left");
    } catch(e) {
        setTimeout( doScrollCheck, 1 );
        return;
    }

    // and execute any waiting functions
    jQuery.ready();
}

return jQuery;

})();

这篇关于getElementById后为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆