Search code examples
formsfunctioncheckboxvbscriptradio-button

Radio button value in VBS


I'm trying to read the value of a radio button in VBS and I am coming up with the following error:

Type mismatch: 'phase'

I am getting this error after I've put the radio buttons in a form in order to enable/disabled the checkbox depending on the first radio button.

Any help much appreciated!

MY CODE (not the whole code, just the faulty part. Sub RunScript is ran by a button):

<head>
</head>

<SCRIPT LANGUAGE="VBScript">

Sub RunScript

Dim currentphase

If phase(0).Checked Then
currentphase = phase(0).Value
End If

If phase(1).Checked Then
currentphase = phase(1).Value
End If


If currentphase = "" Then
MsgBox "Please select the phase.",48,"Error"
Exit Sub
End If

End Sub


</SCRIPT>

<body>
<form name="phaseform" action="" >
<input type="radio" name="phase" value="1" id="phase" onclick="checkbox(0)"/><label for="phase1">Phase 1</label>
<input disabled id=inorout type="checkbox" name="InorOUT" value="IN">LEGAL HOLD (<a href=javascript:RunLogFile()>?</a>) <br>
input type="radio" name="phase" value="2" id="phase" onclick="checkbox(1)" /><label for="phase2">Phase 2 (after 17 days)</label>
</form>

<script type="text/javascript">

function checkbox(val)
{
    if(val)
document.phaseform.InorOUT.setAttribute("disabled",val)
else
document.phaseform.InorOUT.removeAttribute("disabled",val)
}
</script>

</BODY>

Solution

  • Solved by replacing phase(0).Checked with document.phaseform.phase(0).Checked

    Another way: http://us.generation-nt.com/answer/how-can-vbscript-retrieve-value-selected-html-radio-butto-help-151913911.html

    Dim i
    
    for i = 0 to document.phaseform.phase.length - 1
    if document.phaseform.phase(i).checked then
    currentphase = document.phaseform.phase(i).value
    end if
    
    next