2017-08-08 42 views
0

查看減肥代碼。我基本上創建一個項目(打印機)的列表以及一個動態創建的唯一單選按鈕ID,然後我想能夠引用所述無線電ID以切換Sub SetDefaultPrinter中的True/False之間的檢查。爲什麼?因爲使用添加設備/搜索對於我們的一些用戶來說太難了,因此,一個可愛的小GUI。爲什麼動態?因爲我有多個獨立的網絡,我希望腳本根據需要進行調整。如何在HTA(vbscript)中引用動態創建的ID?

<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> 

回答

0

user2345916,我已經修改了你的代碼,其中的變量傳遞像你想。我保留了你的評論,所以你可以拿起你離開的地方。希望這可以幫助!

基本上,您問題的答案在於按鈕的「ID」,「VALUE」和「ONCLICK」值。

  • 的onclick = 'SetDefaultPrinter( 「&我&」)' 將環狀的號碼傳遞到子程序。
  • SetDefaultPrinter(Radioii)從發送給該子例程(在這種情況下,它是0或1)的按鈕的「ONCLICK」字段中設置一個變量。
  • 「FileName = document.getElementById(」Radio「& Radioii).value」獲取按鈕的「VALUE」字段,該字段與在「()」之間設置的「ID」字段相匹配,在您的情況下也是從ONCLICK中提取的變量。
  • 從這裏,你可以使用(文件名)變量做任何你想要的(比賽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>