gpt4 book ai didi

javascript - 在 window.onload 中添加两个函数

转载 作者:IT王子 更新时间:2023-10-29 03:10:37 24 4
gpt4 key购买 nike

我的表单上有两个功能,但如果另一个处于事件状态,则其中一个将不起作用。这是我的代码:

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

我的意思是,当我在我的表单中同时拥有这两个功能时,第二个功能可以正常工作,但第一个功能不起作用,如果去掉第二个功能,第一个功能将正常工作,为什么会这样?是因为名字吗?

最佳答案

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

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

文档是 here

请注意,此解决方案可能无法跨浏览器使用。我认为你需要依赖第三个库,比如 jquery $(document).ready

关于javascript - 在 window.onload 中添加两个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16683176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com