将两个函数添加到window.onload [英] Add two functions to window.onload

查看:99
本文介绍了将两个函数添加到window.onload的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表格上有两个功能,除非一个功能在另一个功能处于活动状态时不起作用。这是我的代码:
$ b $ pre $ window.onload = function(event){
var $ input2 = document.getElementById( '癸');
var $ input1 = document.getElementById('parenta');
$ input1.addEventListener('keyup',function(){
$ input2.value = $ input1.value;
});

$ b $ window.onload = function(){
document.getElementById('enable')。onchange = function(){
var txt = document.getElementById ( 'GOV1');
if(this.checked)txt.disabled = false;
else txt.disabled = true;
};
};

我的意思是当我的表单中有这两个函数时,第二个函数可以正常工作,但首先不会工作,如果拿出第二个功能,第一个功能就会正常工作,为什么会发生这种情况?是因为名字吗?

解决方案

尝试将所有代码放入相同的[并且只有1] onload方法! p>

  window.onload = function(){
//所有代码都在这里
}


I have two functions on my form except one does not work if the other is active. Here is my code:

window.onload = function(event) {
    var $input2 = document.getElementById('dec');
    var $input1 = document.getElementById('parenta');
    $input1.addEventListener('keyup', function() {
        $input2.value = $input1.value;
    });
}

window.onload=function(){
    document.getElementById('enable').onchange=function(){
        var txt = document.getElementById('gov1');
        if(this.checked) txt.disabled=false;
        else txt.disabled = true;
    };
};

What I mean is that when I have both these functions in my form the second function works fine but the first will not work, if take out the second function the first one will work like normal, why is this happening? Is it because of the names?

解决方案

Try putting all you code into the same [and only 1] onload method !

 window.onload = function(){
        // All code comes here 
 }

这篇关于将两个函数添加到window.onload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆