簡體   English   中英

單選按鈕和復選框 javascript

[英]Radio buttons and checkboxes javascript

我有下面的代碼。

2個單選按鈕,選中時啟用+勾選其復選框,單擊另一個單選按鈕時禁用+取消勾選它們。

該代碼作為 HTA 運行良好,但無法作為 HTML 正常運行。 知道最終的應用程序將是 HTA,我是否需要擔心代碼在 HTML 中無法完全運行?

非常感謝。

<table border=1>
<tr>
    <td>
        <form name="phaseform" action=""><font size=2>
        <input type="radio" name="phase" value="1" id="phase" onclick="checkbox(0)" />
            <label for="phase1">Phase 1</label>
    </td>
    <td><font size=2>
        <input type="radio" name="phase" value="2" id="phase" onclick="checkbox(1)" />
            <label for="phase2">Phase 2 (after 17 days)</label>
    </td>
</tr>
<tr>
    <td><font size=2>
        <input type="checkbox" disabled id="TerminateP1" name="TerminateP1" value="IN">Terminate AD account<br>
        <input type="checkbox" disabled id="MailboxAccessP1" name="MailboxAccessP1" value="IN">Grant mailbox access to manager<br>
    </td>
    <td><font size=2>
        <input type="checkbox" disabled id="TerminateP2" name="TerminateP2" value="IN">Fully terminate AD account<br>
        <input type="checkbox" disabled id="DisableMailboxP2" name="DisableMailboxP2" value="IN">Disable mailbox<br>
    </td>
</tr>
</form>

<script type="text/javascript">
function checkbox(val)
    {
        document.phaseform.TerminateP1.setAttribute("disabled",1) 
        document.phaseform.MailboxAccessP1.setAttribute("disabled",1)
        document.phaseform.TerminateP2.setAttribute("disabled",1) 
        document.phaseform.DisableMailboxP2.setAttribute("disabled",1)
        document.phaseform.TerminateP1.setAttribute("checked",0)
        document.phaseform.MailboxAccessP1.setAttribute("checked",0)
        document.phaseform.TerminateP2.setAttribute("checked",0)
        document.phaseform.DisableMailboxP2.setAttribute("checked",0)

        if(val)
        {
            document.phaseform.TerminateP2.removeAttribute("disabled",val) 
            document.phaseform.DisableMailboxP2.removeAttribute("disabled",val)
            document.phaseform.TerminateP2.setAttribute("checked",1)
            document.phaseform.DisableMailboxP2.setAttribute("checked",1)
            }
        else
        {
            document.phaseform.TerminateP1.removeAttribute("disabled",val)
            document.phaseform.MailboxAccessP1.removeAttribute("disabled",val)
            document.phaseform.TerminateP1.setAttribute("checked",1)
            document.phaseform.MailboxAccessP1.setAttribute("checked",1)
        }
    }
</script>

嘗試

function checkbox(val) {
    var isone = !!val, istwo = !val;
    document.phaseform.TerminateP1.disabled = isone;
    document.phaseform.MailboxAccessP1.disabled = isone;
    document.phaseform.TerminateP2.disabled = istwo;
    document.phaseform.DisableMailboxP2.disabled = istwo;
    document.phaseform.TerminateP1.checked = !isone;
    document.phaseform.MailboxAccessP1.checked = !isone;
    document.phaseform.TerminateP2.checked = !istwo;
    document.phaseform.DisableMailboxP2.checked = !istwo;
}

演示:小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM