1

チェックされているラジオ項目に基づいてフォーム要素を有効または無効にする機能があります。正常に動作しています。ただし、適切なフォーム要素が既に無効または有効になっている状態で、ページの読み込み時にラジオボタンの1つがチェックされることを望みます。

現在、ページの読み込み時に、フォーム自体でラジオ ボタンの 1 つをオンにしていますが、変更があると 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>

そして、ここに 2 つのラジオ ボタンがあります。

<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 を起動するために何を変更すればよいかを理解するのに助けが必要です。ありがとうございました。

4

2 に答える 2

4

試す:

window.onload = customerChoice;

あなたが持っている方法は、関数をすぐに実行し、onloadハンドラーをundefined関数自体ではなく結果 (関数は何も返さないため) に設定します。DOM がロードされる前に実行されるため、機能していません。

于 2013-05-06T18:34:58.837 に答える
0

customerChoice()関数を無名関数の中に入れてみてください:

window.onload = function() {
  customerChoice();  
};
于 2013-05-06T18:33:07.390 に答える