JS报告错误-空对象

问题描述:



这小段代码有自己的想法?天空从蓝色开始,当单击粉红色时,它变为粉红色,但是当提交测试"按钮将其修剪为黑暗时,JS会继续报告Null对象box1的错误.

任何人都可以看到我做错了,请给个提示.

PS.在哪里可以找到有关混合JS,ASP和html的很好参考?

Hi,

This little piece of code got a mind of its own? The sky starts with Blue and when the Pink is click, it turns to pink ok, but when submit ''test'' button to trun it to Dark, the JS keep reporting an error of Null object box1.

Any one can see what I did wrong, please give a hint please.

PS. where I can find a good reference for mixing JS, ASP aqnd html?

<script language="javascript" type="text/javascript">
function putText(s) {
alert(s);
    document.f.box1.value = s;
    document.f.submit();
}
</script>

<body>
<%
box1 = request("box1")
if box1 = "" then
    box1 = "The sky is blue today"
    end if

if request("test") = "Dark" then
    response.Write("<script>putText('Submit to get dark sky')</script>")
    end if
%>

<form name="f" id="f" method="post">
<input name="box1" id="box1" type="text" value="<%=box1%>" />
<input type="submit" value="Pink" onClick="putText('Click to Pink Sky')"  />
<input name="test" type="submit" value="Dark"  />
</form>
</body>



rgds,
kfl.



rgds,
kfl.

您的代码存在以下问题:
当您单击暗"按钮时,将调用JavaScript,但此时表单和其他元素不可用.所以它给出了一个错误:document.f.box1 is null or not an object.

要使此代码正常工作,请将if request("test")...部分移至深色按钮声明下面.
我正在为您提供以下工作代码:

The problem with your code is:
When you click the ''Dark'' button, the JavaScript is called but the form and the other elements are not available at that time. So it gives an error: document.f.box1 is null or not an object.

To make this code working, move if request("test")... part to below dark button declaration.
I am giving you the working code below:

<html>
<script language="javascript" type="text/javascript">
function putText(s) {
alert(s);
    document.f.box1.value = s;
    document.f.submit();
}
</script>
    <%
    box1 = request.form("box1")
    if box1 = "" then
        box1 = "The sky is blue today"
    end if
    %>
<body>
    <form name="f" id="f" method="post">
        <input name="box1" id="box1" type="text" value="<%=box1%>" >
        <input type="submit" value="Pink" onClick="putText('Click to Pink Sky')"  >
        <input name="test" id="test" type="submit" value="Dark"  >
        <%
        if request.form("test") = "Dark" then
            response.Write("<script>putText('Submit to get dark sky')</script>")
        end if
        %>
    </form>
</body>
</html>



希望这能解决问题并消除您的疑问.


而且,您甚至不需要在JavaScript函数中两次提交表单.因此删除document.f.submit();从该功能.
[/Edit]



Hope this solves the problem and clears your doubt.


And you don''t even need to submit the form twice in the JavaScript function. So remove document.f.submit(); from the function.
[/Edit]