Javascript onChange()在服务器上不工作,但在JSFIDLE上工作正常?
比较上的代码 JSFIDLE HTML 到 我的网站Javascript onChange()在服务器上不工作,但在JSFIDLE上工作正常?,javascript,html,dependencies,onchange,jsfiddle,Javascript,Html,Dependencies,Onchange,Jsfiddle,比较上的代码 JSFIDLE HTML 到 我的网站 <script> document.getElementById("client_select").onchange = function (e) { if (this.value == 'Name not listed') { document.getElementById("client_add").style.display=""; } else { document.getElementB
<script>
document.getElementById("client_select").onchange =
function (e) {
if (this.value == 'Name not listed') {
document.getElementById("client_add").style.display="";
} else {
document.getElementById("client_add").style.display="none";
}
};
</script>
<select name="client_select" id="client_select">
<option value="Name not listed">Name not listed</option>
<option value="1">abc company</option>
</select>
<span id="client_add"><input type="text" name ="" value="field1"/></span>
document.getElementById(“客户端选择”)。onchange=
职能(e){
如果(this.value=='Name not listed'){
document.getElementById(“客户端添加”).style.display=“”;
}否则{
document.getElementById(“客户端添加”).style.display=“无”;
}
};
姓名未列出
abc公司
解决方案:
将元素加载到客户端DOM后运行javascript
执行以下操作之一:
- 在html元素之后加载javascript
- 使用onLoad()包装脚本
- 对于jQuery,使用$(document).ready(function(){}
<select name="client_select" id="client_select">
<option value="Name not listed">Name not listed</option>
<option value="1">abc company</option>
</select>
<span id="client_add"><input type="text" name ="" value="field1"/></span>
<script>
document.getElementById("client_select").onchange = function(e) {
if (this.value == 'Name not listed') {
document.getElementById("client_add").style.display = "";
} else {
document.getElementById("client_add").style.display = "none";
}
};
</script>
姓名未列出
abc公司
document.getElementById(“客户端选择”).onchange=函数(e){
如果(this.value=='Name not listed'){
document.getElementById(“客户端添加”).style.display=“”;
}否则{
document.getElementById(“客户端添加”).style.display=“无”;
}
};
我还鼓励使用一个简单的解决方案是将脚本包装在
窗口中。onload
函数如下:
window.onload = function(){
// Do things here...
};
或者如果jQuery可用:
$(function(){
// Do things here...
});
我是否遗漏了JSFIDLE默认具有的任何依赖项?我检查了,JSFIDLE似乎没有包含任何内容。当我试图使用Javascript查找对象时,该对象没有加载到DOM中。元素之后是解决方案脚本。是的,谢谢,使用jquery$(document).ready(function(){}^^^不是真的->oooo ^更好\_(ツ)_/“谢谢!
window.onload = function(){
// Do things here...
};
$(function(){
// Do things here...
});