如何在HTA(vbscript)中引用动态创建的ID?

时间:2017-08-08 14:17:05

标签: vbscript hta

查看精简代码。我基本上创建了一个项目列表(打印机)以及一个动态创建的唯一单选按钮ID,然后我希望能够引用所述无线电ID以切换正确/错误之间的检查。 Sub SetDefaultPrinter。为什么?因为使用添加设备/搜索对我们的一些用户来说太难了,因此,一个可爱的小GUI。为什么动态?因为我有多个独立的网络,我更喜欢脚本根据需要自行调整。

<html>
<head>
<title>My HTML application</title>
<HTA:APPLICATION
APPLICATIONNAME="My HTML application"
ID="MyHTMLapplication"
VERSION="1.0"/>
</head>

<script language="VBScript">

Public jj, strPrinters, strModels, strLocations

Sub Window_OnLoad
strPrinters = Array("Printer1", "Printer2")
strModels = Array("HP Color LaserJet 4525", "HP Color LaserJet 4525")
strLocations = Array("Room 1", "Room 2")

jj = UBound(strPrinters)

Call OnClickGo()
End Sub

Sub OnClickGo()
DataArea1.InnerHTML = ""
For i = 0 To jj
        DataArea1.InnerHTML = DataArea1.InnerHTML & "<BR><font style=color:green;font-weight=bold;>" &_
          "<input type=""" & "radio""" & " name=""" &_
          strPrinters(i) & "Radio""" & " id=""" & "Radio" & i & """" &_
          " title=""" & "Clicking here will set " & strPrinters(i) & " as default printer.""" &_
          " onclick=""" & "SetDefaultPrinter(" & i & ")""" & " onmouseover=""" & "Pointer""" &_
          " onmouseout=""" & "DefaultCursor""" & "></input>" &_
          "<span id=""" & strPrinters(i) & "Span""" &_
          " title=""" & "Click here delete printer mapping for " & strPrinters(i) & """" &_
          " onmouseover=""" & "Pointer""" & " onmouseout=""" & "DefaultCursor""" &_
          " onclick=""" & "OnClickDelete(" & i & ")""" &_
          ">" & strPrinters(i) & ", " & strModels(i) & ", Location: " & strLocations(i) & "</span></font>"
Next
End Sub

'========================================
'= Set Default Printer ==================
'========================================
Sub SetDefaultPrinter(ii)
DataArea2.InnerHTML = strPrinters(ii) & " would have been set as default if this was fully functional."
'
' Radio0 and Radio1 are dynamically created IDs, *really* want to somehow
' dynamically reference the dynamically created IDs.
' i.e. something like
'     If ii <> 0 Then (Radio & ii).checked = False
'
If ii <> 0 Then Radio0.checked = False
If ii <> 1 Then Radio1.checked = False
End Sub

'========================================
'= Delete Printer Mapping ===============
'========================================
Sub OnClickDelete(ii)
DataArea2.InnerHTML = strPrinters(ii) & " would have been deleted if this was fully functional."
'Set wshnetwork = CreateObject("WScript.Network")
'wshnetwork.RemovePrinterConnection "\\SERVER\" & strPrinters(PrinterToDelete)
End Sub

'========================================
'= MOUSE Pointers =======================
'========================================
Sub Pointer
document.body.style.cursor = "hand"
End Sub

Sub DefaultCursor
document.body.style.cursor = "default"
End Sub
</script>
<body bgcolor="white">
<span id="DataArea1"></span>
<BR><BR><BR>
<span id="DataArea2"></span>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

user2345916,我修改了你想要的变量传递代码。我完整地留下了你的评论,所以你可以从你离开的地方继续。希望这有帮助!

基本上,问题的答案在于按钮的“ID”,“VALUE”和“ONCLICK”值。

  • ONCLICK ='SetDefaultPrinter(“&amp; i&amp;”)'会将循环号码传递给SubRoutine。
  • SetDefaultPrinter(Radioii)从按钮的“ONCLICK”字段设置一个变量,该字段将您发送到该SubRoutine(在这种情况下,它是0或1)。
  • “FileName = document.getElementById(”Radio“&amp; Radioii).value”获取按钮的“VALUE”字段,该字段与“()”之间设置的“ID”字段匹配, case也是从ONCLICK中提取的变量。
  • 从这里开始,您可以使用(FileName)变量做任何你想做的事情(匹配IF / THEN等)

    <script language=vbscript>
    Sub Window_OnLoad
    window.resizeTo 500,300
    strPrinters = Array("Printer 1", "Printer 2")
    strModels = Array("HP Color LaserJet 4525", "HP Color LaserJet 4525")
    strLocations = Array("Room 1", "Room 2")
    
    jj = UBound(strPrinters)
    For i = 0 To jj
    strHTML1 = "<span id='Delete" & i & "' value='" & strPrinters(i) & "'title='Click here delete printer mapping for " & strPrinters(i) & "' onmouseover='Pointer' onmouseout='DefaultCursor' onclick='OnClickDelete(" & i & ")'> " & strPrinters(i) & " - " & strModels(i) & ", Location: " & strLocations(i) & "</span>"
    strHTML2 = strHTML2 & "<input type='radio' name='radio' value='" & strPrinters(i) & "' id='Radio" & i & "' title='Clicking here will set " & strPrinters(i) & " as default printer.' onclick='SetDefaultPrinter(" & i & ")' onmouseover='Pointer' onmouseout='DefaultCursor'>" &_
    "" & strHTML1 & "</input><br>"
    
    DataArea1.InnerHTML = strHTML2
    Next
    End Sub
    
    '========================================
    '= Set Default Printer ==================
    '========================================
    Sub SetDefaultPrinter(Radioii)
    FileName = document.getElementById("Radio" & Radioii).value
    DataArea3.InnerHTML = Filename & " would have been set as default if this was fully functional."
    '
    ' Radio0 and Radio1 are dynamically created IDs, *really* want to somehow
    ' dynamically reference the dynamically created IDs.
    ' i.e. something like
    '     If ii <> 0 Then (Radio & ii).checked = False
    '
    If Radioii = 0 Then Radio0 = False
    If Radioii = 1 Then Radio1 = False
    End Sub
    
    '========================================
    '= Delete Printer Mapping ===============
    '========================================
    Sub OnClickDelete(Deleteii)
    RemoveName = document.getElementById("Delete" & Deleteii).value
    DataArea3.InnerHTML = RemoveName & " would have been deleted if this was fully functional."
    'Set wshnetwork = CreateObject("WScript.Network")
    'wshnetwork.RemovePrinterConnection "\\SERVER\" & strPrinters(PrinterToDelete)
    End Sub
    
    '========================================
    '= MOUSE Pointers =======================
    '========================================
    Sub Pointer
    document.body.style.cursor = "hand"
    End Sub
    
    Sub DefaultCursor
    document.body.style.cursor = "default"
    End Sub
    </script>