已检查javascript加载页面广播

已检查javascript加载页面广播,javascript,forms,Javascript,Forms,我有一个函数,可以启用或禁用表单元素,并根据该元素检查无线电项目。它工作正常。但是,我希望在页面加载时选中其中一个单选按钮,并禁用或启用相应的表单元素 现在在页面加载中,我在表单本身上检查了一个单选按钮,但是javascript会在发生更改时触发 下面是javascript <script type="text/javascript"> function customerChoice() { if (document.getElementByI

我有一个函数,可以启用或禁用表单元素,并根据该元素检查无线电项目。它工作正常。但是,我希望在页面加载时选中其中一个单选按钮,并禁用或启用相应的表单元素

现在在页面加载中,我在表单本身上检查了一个单选按钮,但是javascript会在发生更改时触发

下面是javascript

<script type="text/javascript">
        function customerChoice() {
            if (document.getElementById('radio1').checked) {
                document.getElementById('company').disabled = false;
                document.getElementById('onetime').disabled = true;
                document.getElementById('span1').style.color = '#cccccc';
                document.getElementById('span2').style.color = '#000000';
                }
            if (document.getElementById('radio2').checked) {
                document.getElementById('company').disabled = true;
                document.getElementById('onetime').disabled = false;
                document.getElementById('span1').style.color = '#000000';
                document.getElementById('span2').style.color = '#cccccc';
            }
        }
window.onload = customerChoice();

</script>

函数customerChoice(){
if(document.getElementById('radio1')。选中){
document.getElementById('company')。disabled=false;
document.getElementById('onetime').disabled=true;
document.getElementById('span1').style.color='#cccc';
document.getElementById('span2').style.color='#000000';
}
if(document.getElementById('radio2')。选中){
document.getElementById('company').disabled=true;
document.getElementById('onetime')。disabled=false;
document.getElementById('span1').style.color='#000000';
document.getElementById('span2').style.color='#cccc';
}
}
window.onload=customerChoice();
这是两个单选按钮

<input type="radio" name="type" id="radio1" value="C" onclick="customerChoice()" checked />Current Customer<br />

<input type="radio" name="type" id="radio2" value="O" onclick="customerChoice()" />One Time Customer<br />
当前客户
一次性客户

需要帮助确定要更改什么才能使javascript在加载时启动。谢谢。

尝试将
customerChoice()
函数放入匿名函数中:

window.onload = function() {
  customerChoice();  
};

尝试将
customerChoice()
函数放入匿名函数中:

window.onload = function() {
  customerChoice();  
};
尝试:

您让它立即运行函数,并将
onload
处理程序设置为结果(这是
未定义的
,因为函数不返回任何内容),而不是函数本身。它无法工作,因为它在加载DOM之前运行。

请尝试:


您让它立即运行函数,并将
onload
处理程序设置为结果(这是
未定义的
,因为函数不返回任何内容),而不是函数本身。它不工作,因为它在加载DOM之前运行。

我正要添加相同的答案…结尾的括号导致函数立即运行,而不是在页面加载完成时。我正要添加相同的答案…结尾的括号导致函数立即运行,而不是当页面完成加载时。