在該例中,如果你打算取消對一個單選框的選取,你必須點擊另一個單選框。再看下面的程序:
    <form name="form_1">
        <input type="radio" name ="radio_1" onClick="offButton();">Light off
        <input type="radio" name ="radio_2" onClick="onButton();" checked>Light on
    </form>
當第一個單選框被選中時,函數offButton() 被調用。函數如下:
    function offButton()
    {
        var the_box = window.document.form_1.radio_1;
        if (the_box.checked == true) 
        {
            window.document.form_1.radio_2.checked = false;
            document.bgColor='black';
            alert("Hey! Turn that back on!");
        }
    }
這個例子很象前面的復選框例子,主要的區別在于該行:
window.document.form_1.radio_2.checked = false;
該行指令指示JavaScript在該按鈕被點擊時關閉另外一個按鈕。由于另外一個按鈕的函數同這個很相似:
    function onButton()
    {
        var the_box = window.document.form_1.radio_2;
        if (the_box.checked == true) 
        {
            window.document.form_1.radio_1.checked = false;
            document.bgColor='white';
            alert("Thanks!");
        }
    }
新聞熱點
疑難解答