为什么我会收到这个Javascript错误“;未定义连接”;
Why am I getting this Javascript error "connection is not defined"?
我不确定为什么会出现这个错误:
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" />
首先,将表单存储为本地变量:
var form = document.getElementById("formTest");
然后使用这个局部变量,而不是多次查询DOM:
var hasConnection = form.connection.value.length != 0;
var hasLocation = form.location.value.length != 0;
最后,在connection
前面加上form.
:
document.getElementById("flashTest").sendValFromHtml(form.connection.value);
替换:
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;
将connection.value
更改为:
document.forms.formTest.connection.value
相关文章:
- webRTC错误未定义配置
- gmaps4rails 2.4.6未捕获引用错误:未定义Gmaps
- 未捕获引用错误:未定义validateform
- 为什么我会出现此错误"未捕获引用错误:未定义标题;
- Sitecore”;未捕获引用错误:未定义MultilistWithSearch;
- Wordpress:未捕获引用错误:未定义速度
- Node.js引用错误:未定义类名
- Replace()产生“;未捕获的类型错误:未定义的不是函数“;当与零一起使用时
- 错误-未定义easeInBounce
- 未捕获的引用错误:未定义<映射键>
- 未捕获的类型错误:未定义不是上的函数.GetElementByID
- Javascript错误:未捕获引用错误:未定义编辑
- javascript onchange取消绑定错误|未定义
- requirejs优化器错误:未定义窗口
- 未捕获引用错误:未定义PrintThisPage
- JSHint 错误:未定义我的应用程序 (W117)
- 当我编写脚本时,它显示引用错误:未定义宽度
- socket.io - 引用错误:未定义 io
- 反应.js:未捕获类型错误:未定义不是一个函数
- 出现“未捕获的类型错误:未定义不是函数”控制台错误