2012年8月12日日曜日

VB用Elements値取得関数


getElementsByName等で取得したInput等のElementsから値を取得する関数です。
input(text,password,file,hidden,checkbox,radio),select,textareaに対応しています。
異なるtype(tag)が混在しているElementsを渡した場合に正しく動作しない事があります。
'エレメントから値を取得
Function getElementsValue(ByRef objElms)
    getElementsValue = ""
    If objElms(0).type = "checkbox" Then
        getElementsValue = getCheckedElementsValue(objElms)
    ElseIf objElms(0).type = "radio" Then
        getElementsValue = getSelectedElementValue(objElms)
    Else
        getElementsValue = getTextElementsValue(objElms)
    End If
End Function

'text,password,select,file,hidden,textarea
Function getTextElementsValue(ByRef objElms)
    getTextElementsValue = ""
    Dim lngIu: lngIu = objElms.Length - 1
    If lngIu = 0 Then
        getTextElementsValue = objElms(0).Value
    ElseIf lngIu > 0 Then
        Dim strVals: ReDim strVals(lngIu)
        Dim lngI
        For lngI = 0 To lngIu
            strVals(lngI) = objElms(lngI).Value
        Next
        getTextElementsValue = strVals
    End If
End Function

'checkbox(選択されているものの値を取得)
Function getCheckedElementsValue(ByRef objElms)
    getCheckedElementsValue = ""
    Dim lngIu: lngIu = objElms.Length - 1
    If lngIu = 0 Then
        If objElms(0).Checked Then
            getCheckedElementsValue = objElms(0).Value
        End If
    ElseIf lngIu > 0 Then
        Dim strVals: ReDim strVals(lngIu)
        Dim lngI
        Dim lngSize: lngSize = 0
        For lngI = 0 To lngIu
            If objElms(lngI).Checked Then
                strVals(lngSize) = objElms(lngI).Value
                lngSize = lngSize + 1
            End If
        Next
        ReDim Preserve strVals(lngSize - 1)
        getCheckedElementsValue = strVals
    End If
End Function

'checkbox(選択されているかどうかを取得)
Function getElementsChecked(ByRef objElms)
    getElementsChecked = False
    Dim lngIu: lngIu = objElms.Length - 1
    If lngIu = 0 Then
        getElementsChecked = objElms(0).Checked
    ElseIf lngIu > 0 Then
        Dim strVals: ReDim strVals(lngIu)
        Dim lngI
        For lngI = 0 To lngIu
            strVals(lngI) = objElms(lngI).Checked
        Next
        getElementsChecked = strVals
    End If
End Function

'radio
Function getSelectedElementValue(ByRef objElms)
    getSelectedElementValue = ""
    Dim lngIu: lngIu = objElms.Length - 1
    Dim lngI
    For lngI = 0 To lngIu
        If objElms(lngI).Checked Then
            getSelectedElementValue = objElms(lngI).Value
        End If
    Next
End Function


0 件のコメント:

コメントを投稿