简体   繁体   中英

Why am I getting this Javascript error “connection is not defined”?

I'm not sure why I'm getting this error:

connection is not defined   
document.getElementById("flashTest").sendValFromHtml(connection.value);

This is my code:

function submitCheck() {
    var hasConnection = document.getElementById("formTest").connection.value.length != 0; 
    var hasLocation = document.getElementById("formTest").location.value.length != 0;

    document.getElementById("connection").className = hasConnection ? "" : "invalid";
    document.getElementById("location").className = hasLocation ? "" : "invalid";

    if(hasConnection && hasLocation){
        document.getElementById("flashTest").sendValFromHtml(connection.value);
    }
}

HTML:

<form id="formTest" name="formTest" method="post" action="">
    <fieldset class="form">
        <div class="connection">
            <label id="connection">Connection:*</label>
            <div class="textwrapper">
                <select name="connection">
                    <option value="">Select connection type</option>
                    <option value="dsl">DSL</option>
                    <option value="cable">Cable</option>
                    <option value="fibre">Fibre</option>
                </select>
            </div>
        </div>
        <div class="location">
            <label id="location">Location*:</label>
            <div class="textwrapper">
                <select name="location">
                    <option value="">Select your location</option>
                    <option value="home">At home</option>
                    <option value="work">At work</option>
                </select>
            </div>
        </div>
        <div class="postcode">
            <label>Postcode:</label>
            <div class="textwrapper">
                <input type="text" name="postcodeVal" id="postcodeVal">
            </div>
        </div>
        <div class="start clear"> 
        <input type="button" name="sendToFlash" id="sendToFlash" value="Start Test" onclick="submitCheck();" />

        </div>
    </fieldset>
</form>

<embed src="/flash/speedtest.swf" id="flashTest" name="flashTest" width="540" height="320" allowscriptaccess="always" type="application/x-shockwave-flash"  flashvars="jsfunc=pushResults&jsfunc2=showExtras" />

First, store the form as a local variable:

var form = document.getElementById("formTest");

Then use this local variable instead of querying the DOM multiple times:

var hasConnection = form.connection.value.length != 0; 
var hasLocation = form.location.value.length != 0;

Lastly, prefix connection with form. :

document.getElementById("flashTest").sendValFromHtml(form.connection.value);

Replace:

var hasConnection = document.getElementById("formTest").connection.value.length != 0; 
var hasLocation = document.getElementById("formTest").location.value.length != 0;

with:

var hasConnection = document.getElementById("connection").value.length != 0; 
var hasLocation = document.getElementById("location").value.length != 0;

connection.value更改为:

document.forms.formTest.connection.value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM