チェックされているラジオ項目に基づいてフォーム要素を有効または無効にする機能があります。正常に動作しています。ただし、適切なフォーム要素が既に無効または有効になっている状態で、ページの読み込み時にラジオボタンの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 を起動するために何を変更すればよいかを理解するのに助けが必要です。ありがとうございました。