Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript onChange()在服务器上不工作,但在JSFIDLE上工作正常?_Javascript_Html_Dependencies_Onchange_Jsfiddle - Fatal编程技术网

Javascript onChange()在服务器上不工作,但在JSFIDLE上工作正常?

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

比较上的代码

JSFIDLE

HTML

我的网站

<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(){}


当您试图使用该元素时,该元素不可用,因为它尚未加载到DOM中

将脚本移动到元素之后

<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...
});