2012-02-07 67 views
2

我不知道爲什麼我收到此錯誤:爲什麼我得到這個Javascript錯誤「連接沒有定義」?

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

這是我的代碼:

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" /> 
+0

它不會出現連接被定義任何地方根據您向我們展示的代碼 – kinakuta 2012-02-07 06:15:22

+0

你能張貼您的HTML代碼也? – 2012-02-07 06:18:03

+0

「postcodeVal」定義在哪裏? – 2012-02-07 06:20:57

回答

3

首先,形式存儲爲一個局部變量:

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

然後使用,而不是DOM多次查詢這個局部變量:

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

最後,前綴connectionform.

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

謝謝!完善! – muudless 2012-02-07 06:30:41

2

替換:

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

附:

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

我試過你的解決方案,現在我得到'document.getElementById(「connection」)。value is undefined'\t 'var hasConnection = document.getElementById(「connection」).value.length!= 0;' – muudless 2012-02-07 06:23:37

+0

2'select'標籤沒有'id'屬性,所以你不能通過使用'document.getElementById()'函數來獲取它們的值 – 2012-02-07 06:37:56

1

更改connection.value到:

document.forms.formTest.connection.value 
相關問題