getElementById后是否为null?

发布于 2024-12-02 11:55:38 字数 893 浏览 8 评论 0 原文

我确信这里有一个非常简单的错误,但基本上我有一个 id 为 formID 的表单,我像这样调用它:

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

我收到错误:form is null

有人能发现错误吗?

(PS 将其称为表单的提交)

更新 好吧,基本上我有两个 onsubmit 一个表单,这显然不起作用。所以我正在做的是从另一个函数中调用这个函数...

这是表单标签:

<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:

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

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

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"),

And I'm getting the error: form is null

Can anyone spot the error?

(PS calling it onsubmit of a form)

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...

Heres the form tag:

<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">

And heres RememberFormFields:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

巴黎盛开的樱花 2024-12-09 11:55:38

他在提交表单时调用该函数,因此在调用该函数时肯定已经加载了表单。
(请参见此处 http://jsfiddle.net/HaajY/

我宁愿在id 或类似的东西。
但需要看到更多才能发现问题。

更新:
您需要将元素作为参数传递给函数。

这将起作用:

<script type="text/javascript">
function redvalidate(elem) {
var fields = elem.getElementsByClassName("validate")
alert(fields)
}

function RememberFormFields(elem,form,list)
{
    redvalidate(elem);
}
</script>
<form name="purchaseform" id="formID" onSubmit="RememberFormFields(this,'purchaseform','name,email,ship_to,phone_extension,pi_name');" method="post" enctype="multipart/form-data">
    <input type="submit"/>
</form>

在此处查看它的实际情况:http://jsfiddle.net/HaajY/2/

He's calling the function on submit of the form, so the form definitely has been loaded when the function is called.
(see here http://jsfiddle.net/HaajY/)

I'd rather go for a misspelling in the id or something like that.
Need to see more to be able to spot the problem though.

Update:
You need to pass in the element as a param to the function.

this will work:

<script type="text/javascript">
function redvalidate(elem) {
var fields = elem.getElementsByClassName("validate")
alert(fields)
}

function RememberFormFields(elem,form,list)
{
    redvalidate(elem);
}
</script>
<form name="purchaseform" id="formID" onSubmit="RememberFormFields(this,'purchaseform','name,email,ship_to,phone_extension,pi_name');" method="post" enctype="multipart/form-data">
    <input type="submit"/>
</form>

see it in action here : http://jsfiddle.net/HaajY/2/

寻梦旅人 2024-12-09 11:55:38

假设您的页面上有 html

,您只需要推迟 javascript 的加载,直到 DOM 准备就绪。您试图从尚未应用于 DOM 的元素获取 ID,因此 JavaScript 将其视为 NULL。

有很多方法可以实现这一目标。你可以把所有的JS放在标签附近,你可以使用Google 建议的延迟 JavaScript 加载实践,它将所有 JS 从正文底部附加到 中。

这是 JQuery 如此方便的原因之一,因为您可以将代码包装在 $(document).ready(function(){}); 块中,这样任何 DOM 操作脚本都将始终有效,因为它们等到 DOM 创建完毕后再触发。它通过在现代浏览器中查找“DOMContentLoaded”事件来实现这一点,并为 IE 提供一个 doScrollCheck() 函数,该函数尝试一遍又一遍地滚动正文 - 如果它是可滚动的,他们知道最好去。如果您不想为此加载 JQuery,则可以轻松复制此实践。

JQuery 的 DOM 就绪检查:

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;

})();

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.

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.

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'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;

})();
酒绊 2024-12-09 11:55:38

您还可以有嵌套的表单,即表单内的表单。

<form>
    <form id="nested-form">
        <!-- html -->   
    </form>
</form>

在这种情况下,使用 document.getElementById("nested-form") 无法检测到嵌套表单,并且该函数将返回 null

You could also have nested forms, form inside a form.

<form>
    <form id="nested-form">
        <!-- html -->   
    </form>
</form>

In this case the nested form is not detectable with document.getElementById("nested-form") and the function will return null.

怪我闹别瞎闹 2024-12-09 11:55:38

这只是因为该函数在 DOM 对象加载之前执行。使用 body 标签的“onload”函数来调用您的函数。或者使用 jQuery 的 $(document).ready() 函数来处理所有事情。

This is only because of the function getting executed before the DOM object is loaded. Use "onload" function of the body tag to call your function. Or use jQuery's $(document).ready() function which takes care of everything.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文