hta vbscript 读取文本文件,拆分数据并填充下拉框,删除空白选项
hta vbscript read text files, split data and populate drop down box, remove blank option
以下示例中有 3 个文件。
执行 dropdownboxtest.hta 显示 msgboxes 中所需的正确数据。
问题是下拉框:
IP 数据填写正确,下拉菜单填写正确(所有数据都存在)。问题是第一个选项是空白的,我试图用这个例子"colin","selected"选项使用代码:
document.getElementById("savedhostname").value = last_hostname_connected_value
或
document.getElementById("savedhostname").selected = last_hostname_connected_value
saved_last.cfg
last_hostname_connected = "colin"
last_IP_connected = "192.168.1.8"
savedhosts.txt
andy;192.168.1.5
brian;192.168.1.6
colin;192.168.1.8
david;192.168.1.15
eddie;192.168.1.54
dropdownboxtest.hta
<!DOCTYPE html>
<html>
<head>
<title>Test Dropdown Box</title>
<HTA:APPLICATION
ID="objHTA_Info"
APPLICATIONNAME="Test Dropdown Box"
SINGLEINSTANCE="no"
SCROLL="no"
BORDER="Thin"
BorderStyle="Raised"
Icon="#"
MaximizeButton="No"
>
<script language="javascript">
window.resizeTo (810, 634);
window.moveTo((screen.width - 10) / 2, (screen.height - 634) / 2);
</script>
<SCRIPT LANGUAGE="VBScript">
Dim last_hostname_connected_value, last_IP_connected_value
Sub Window_onLoad
With (CreateObject("Scripting.FileSystemObject"))
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
If .FileExists("saved_last.cfg") Then
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ReadTextFile = FSO.OpenTextFile("saved_last.cfg", 1) ' Open text with read only mode
Do Until ReadTextFile.AtEndOfStream
Textline = ReadTextFile.Readline()
If Instr(Textline, "last_hostname_connected =") Then
last_hostname_connected_value = Split(Textline, "=")(1)
last_hostname_connected_value = Replace(last_hostname_connected_value, Chr(34), "")
last_hostname_connected_value = LTrim(last_hostname_connected_value)
MsgBox "last_hostname_connected_value = " & last_hostname_connected_value
End If
If Instr(Textline, "last_IP_connected =") Then
last_IP_connected_value = Split(Textline, "=")(1)
last_IP_connected_value = Replace(last_IP_connected_value, Chr(34), "")
last_IP_connected_value = LTrim(last_IP_connected_value)
MsgBox "last_IP_connected_value : " & last_IP_connected_value
End If
Loop
End If
End With
Set obj = CreateObject("Scripting.FileSystemObject") 'Creating a File Object
Const ForReading = 1 'Defining Constant Value to read from a file
Set obj1 = obj.OpenTextFile("savedhosts.txt", ForReading) 'Opening a text file and reading text from it
Dim str,str1,objOption
'str=obj1.ReadAll 'All text from the file is read using ReadAll
'Msgbox str 'Contents of a file will be displayed through message box
'Do while obj1.AtEndofStream 'Reading text line wise using Do Loop and ReadLine
' str1=obj1.ReadLine
' Msgbox str1
'Loop
Call ClearHostnameListbox()
Do Until obj1.AtEndOfStream
strNextLine = obj1.ReadLine
arrServiceList = Split(strNextLine, ";")
'MsgBox "arrServiceList(0) = " & arrServiceList(0)
'MsgBox "arrServiceList(1) = " & arrServiceList(1)
Set objOption = document.createElement("OPTION")
objOption.Text = arrServiceList(0)
objOption.Value = arrServiceList(1)
savedhostname.Add(objOption)
'document.getElementById("savedhostname").value=77747
Loop
'MsgBox "arrServiceList(0) = " & arrServiceList(0)
'MsgBox "arrServiceList(1) = " & arrServiceList(1)
obj1.Close 'Closing a File
Set obj=Nothing 'Releasing File object
document.getElementById("savedhostname").value=last_hostname_connected_value
MsgBox "End of SUB check last_hostname_connected_value = " & last_hostname_connected_value
document.getElementById("IP-Input").value = last_IP_connected_value
End Sub
</script>
<SCRIPT LANGUAGE="VBScript">
Sub ClearHostnameListbox()
For Each objOption in savedhostname.Options
objOption.RemoveNode
Next
End Sub
</script>
</head>
<body>
TEST
<br>
IP: <input type='text' maxlength="15" class="enterhostip" size=22 id="IP-Input" name="hostname_IP" />
<br>
Saved Name : <select id="savedhostname" selected="savedhostname" maxlength="20" name="savedhostname" class="savedhostname" value=""></select>
</body>
</html>
显然这里发生了一些我似乎无法确定的事情,我们将不胜感激任何帮助。
这样试试:
<!DOCTYPE html>
<html>
<head>
<title>Test Dropdown Box</title>
<HTA:APPLICATION
ID="objHTA_Info"
APPLICATIONNAME="Test Dropdown Box"
SINGLEINSTANCE="no"
SCROLL="no"
BORDER="Thin"
BorderStyle="Raised"
Icon="#"
MaximizeButton="No"
>
<script language="javascript">
window.resizeTo (810, 634);
window.moveTo((screen.width - 10) / 2, (screen.height - 634) / 2);
</script>
<SCRIPT LANGUAGE="VBScript">
Dim last_hostname_connected_value, last_IP_connected_value
Sub Window_onLoad
With (CreateObject("Scripting.FileSystemObject"))
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
If .FileExists("saved_last.cfg") Then
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ReadTextFile = FSO.OpenTextFile("saved_last.cfg", 1) ' Open text with read only mode
Do Until ReadTextFile.AtEndOfStream
Textline = ReadTextFile.Readline()
If Instr(Textline, "last_hostname_connected =") Then
last_hostname_connected_value = Split(Textline, "=")(1)
last_hostname_connected_value = Replace(last_hostname_connected_value, Chr(34), "")
last_hostname_connected_value = LTrim(last_hostname_connected_value)
'MsgBox "last_hostname_connected_value = " & last_hostname_connected_value
End If
If Instr(Textline, "last_IP_connected =") Then
last_IP_connected_value = Split(Textline, "=")(1)
last_IP_connected_value = Replace(last_IP_connected_value, Chr(34), "")
last_IP_connected_value = LTrim(last_IP_connected_value)
'MsgBox "last_IP_connected_value : " & last_IP_connected_value
End If
Loop
End If
End With
Call ClearHostnameListbox()
Set obj = CreateObject("Scripting.FileSystemObject") 'Creating a File Object
Const ForReading = 1 'Defining Constant Value to read from a file
Set obj1 = obj.OpenTextFile("savedhosts.txt", ForReading) 'Opening a text file and reading text from it
Dim str,str1,objOption
'str=obj1.ReadAll 'All text from the file is read using ReadAll
'Msgbox str 'Contents of a file will be displayed through message box
'Do while obj1.AtEndofStream 'Reading text line wise using Do Loop and ReadLine
' str1=obj1.ReadLine
' Msgbox str1
'Loop
Do Until obj1.AtEndOfStream
strNextLine = obj1.ReadLine
arrServiceList = Split(strNextLine, ";")
'MsgBox "arrServiceList(0) = " & arrServiceList(0)
'MsgBox "arrServiceList(1) = " & arrServiceList(1)
Set objOption = document.createElement("OPTION")
objOption.Text = arrServiceList(0)
objOption.Value = arrServiceList(1)
savedhostname.Add(objOption)
'document.getElementById("savedhostname").value=77747
Loop
'MsgBox "arrServiceList(0) = " & arrServiceList(0)
'MsgBox "arrServiceList(1) = " & arrServiceList(1)
obj1.Close 'Closing a File
Set obj=Nothing 'Releasing File object
'document.getElementById("savedhostname").value=last_hostname_connected_value
'MsgBox "End of SUB check last_hostname_connected_value = " & last_hostname_connected_value
document.getElementById("IP-Input").value = last_IP_connected_value
End Sub
Sub ClearHostnameListbox()
For Each objOption in savedhostname.Options
objOption.RemoveNode
Next
End Sub
</script>
</head>
<body>
TEST
<br>
IP: <input type='text' maxlength="15" class="enterhostip" size=22 id="IP-Input" name="hostname_IP" />
<br>
Saved Name : <select id="savedhostname" selected="savedhostname" maxlength="20" name="savedhostname" class="savedhostname"></select>
</body>
</html>
以下乱七八糟的代码有效但由于一个原因而失败。
工作:
- 加载后,它使用最后已知的 Name/IP 并相应地填充 IP 文本和名称。
- 空行没了
- 从下拉框中选择另一个值会自动更改 IP 文本字段。
- IP 值已正确写入 "saved_last.cfg" 文件。
不工作:
- 它写入的主机名值 "saved_last.cfg" 不需要。
当您从下拉菜单中选择时,它会将 IP 地址写入 "last_hostname_connected_value" 而不是主机名。
<!DOCTYPE html>
<html>
<head>
<title>Test Dropdown Box</title>
<HTA:APPLICATION
ID="objHTA_Info"
APPLICATIONNAME="Test Dropdown Box"
SINGLEINSTANCE="no"
SCROLL="no"
BORDER="Thin"
BorderStyle="Raised"
Icon="#"
MaximizeButton="No"
>
<script language="javascript">
window.resizeTo (810, 634);
window.moveTo((screen.width - 10) / 2, (screen.height - 634) / 2);
</script>
<SCRIPT LANGUAGE="VBScript">
Dim last_hostname_connected_value, last_IP_connected_value
Sub Window_onLoad
'MsgBox "Start of SUB check last_hostname_connected_value = " & last_hostname_connected_value
With (CreateObject("Scripting.FileSystemObject"))
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
If .FileExists("saved_last.cfg") Then
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ReadTextFile = FSO.OpenTextFile("saved_last.cfg", 1) ' Open text with read only mode
Do Until ReadTextFile.AtEndOfStream
Textline = ReadTextFile.Readline()
If Instr(Textline, "last_hostname_connected =") Then
last_hostname_connected_value = Split(Textline, "=")(1)
last_hostname_connected_value = Replace(last_hostname_connected_value, Chr(34), "")
last_hostname_connected_value = LTrim(last_hostname_connected_value)
'MsgBox "last_hostname_connected_value = " & last_hostname_connected_value
End If
If Instr(Textline, "last_IP_connected =") Then
last_IP_connected_value = Split(Textline, "=")(1)
last_IP_connected_value = Replace(last_IP_connected_value, Chr(34), "")
last_IP_connected_value = LTrim(last_IP_connected_value)
'MsgBox "last_IP_connected_value : " & last_IP_connected_value
End If
Loop
End If
End With
Set obj = CreateObject("Scripting.FileSystemObject") 'Creating a File Object
Const ForReading = 1 'Defining Constant Value to read from a file
Set obj1 = obj.OpenTextFile("savedhosts.txt", ForReading) 'Opening a text file and reading text from it
Dim str,str1,objOption
'str=obj1.ReadAll 'All text from the file is read using ReadAll
'Msgbox str 'Contents of a file will be displayed through message box
'Do while obj1.AtEndofStream 'Reading text line wise using Do Loop and ReadLine
' str1=obj1.ReadLine
' Msgbox str1
'Loop
Call ClearHostnameListbox()
Do Until obj1.AtEndOfStream
strNextLine = obj1.ReadLine
arrServiceList = Split(strNextLine, ";")
'MsgBox "arrServiceList(0) = " & arrServiceList(0)
'MsgBox "arrServiceList(1) = " & arrServiceList(1)
Set objOption = document.createElement("OPTION")
objOption.Text = arrServiceList(0)
objOption.Value = arrServiceList(1)
OptionChooser = last_hostname_connected_value
savedhostname.Add(objOption)
'document.getElementById("savedhostname").select=77747
Loop
'MsgBox "arrServiceList(0) = " & arrServiceList(0)
'MsgBox "arrServiceList(1) = " & arrServiceList(1)
obj1.Close 'Closing a File
Set obj=Nothing 'Releasing File object
'MsgBox "1PreEnd of SUB check last_hostname_connected_value = " & last_hostname_connected_value
'last_hostname_connected_value=document.getElementById("savedhostname").value
'document.getElementById("savedhostname").value=last_hostname_connected_value
'MsgBox "2PreEnd of SUB check last_hostname_connected_value = " & last_hostname_connected_value
'document.getElementById("savedhostname").selected=david
'MsgBox "End of SUB check last_hostname_connected_value = " & last_hostname_connected_value
document.getElementById("IP-Input").value = last_IP_connected_value
'document.getElementById("IP-Input").value = last_hostname_connected_value
'document.getElementById("savedhostname").selected = last_hostname_connected_value
document.getElementById("savedhostname").value = last_IP_connected_value
End Sub
</script>
<SCRIPT LANGUAGE="VBScript">
Sub ClearHostnameListbox()
For Each objOption in savedhostname.Options
objOption.RemoveNode
Next
End Sub
Sub changeNamechangeIP
last_hostname_connected_value=document.getElementById("savedhostname").value
'MsgBox "last_hostname_connected_value = " & last_hostname_connected_value
last_IP_connected_value=document.getElementById("IP-Input").value
'MsgBox "last_IP_connected_value = " & last_IP_connected_value
Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Const ForWriting = 2
Dim myFile,myTemp,objFSO,objFile,strText,strNewText,objRegEx
Set myFile = objFSO.OpenTextFile("saved_last.cfg", ForReading, True)
Set myTemp= objFSO.OpenTextFile("saved_last.cfg" & ".tmp", ForWriting, True)
Do While Not myFile.AtEndofStream
myLine = myFile.ReadLine
If InStr(myLine, "last_IP_connected = ") Then
myLine = "last_IP_connected = " & Chr(34) & last_hostname_connected_value & Chr(34)
End If
If InStr(myLine, "last_hostname_connected = ") Then
myLine = "last_hostname_connected = " & Chr(34) & last_hostname_connected_value & Chr(34)
End If
myTemp.WriteLine myLine
Loop
myFile.Close
myTemp.Close
objFSO.DeleteFile("saved_last.cfg")
objFSO.MoveFile "saved_last.cfg" & ".tmp", "saved_last.cfg"
With (CreateObject("Scripting.FileSystemObject"))
'Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
If .FileExists("saved_last.cfg") Then
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ReadTextFile = FSO.OpenTextFile("saved_last.cfg", 1) ' Open text with read only mode
Do Until ReadTextFile.AtEndOfStream
Textline = ReadTextFile.Readline()
If Instr(Textline, "last_hostname_connected =") Then
last_hostname_connected_value = Split(Textline, "=")(1)
last_hostname_connected_value = Replace(last_hostname_connected_value, Chr(34), "")
last_hostname_connected_value = LTrim(last_hostname_connected_value)
'MsgBox "last_hostname_connected_value = " & last_hostname_connected_value
End If
If Instr(Textline, "last_IP_connected =") Then
last_IP_connected_value = Split(Textline, "=")(1)
last_IP_connected_value = Replace(last_IP_connected_value, Chr(34), "")
last_IP_connected_value = LTrim(last_IP_connected_value)
'MsgBox "last_IP_connected_value : " & last_IP_connected_value
End If
Loop
End If
End With
Set obj = CreateObject("Scripting.FileSystemObject") 'Creating a File Object
'Const ForReading = 1 'Defining Constant Value to read from a file
Set obj1 = obj.OpenTextFile("savedhosts.txt", ForReading) 'Opening a text file and reading text from it
Dim str,str1,objOption
'str=obj1.ReadAll 'All text from the file is read using ReadAll
'Msgbox str 'Contents of a file will be displayed through message box
'Do while obj1.AtEndofStream 'Reading text line wise using Do Loop and ReadLine
' str1=obj1.ReadLine
' Msgbox str1
'Loop
Call ClearHostnameListbox()
Do Until obj1.AtEndOfStream
strNextLine = obj1.ReadLine
arrServiceList = Split(strNextLine, ";")
'MsgBox "arrServiceList(0) = " & arrServiceList(0)
'MsgBox "arrServiceList(1) = " & arrServiceList(1)
Set objOption = document.createElement("OPTION")
objOption.Text = arrServiceList(0)
objOption.Value = arrServiceList(1)
OptionChooser = last_hostname_connected_value
savedhostname.Add(objOption)
'document.getElementById("savedhostname").select=77747
Loop
'MsgBox "arrServiceList(0) = " & arrServiceList(0)
'MsgBox "arrServiceList(1) = " & arrServiceList(1)
obj1.Close 'Closing a File
Set obj=Nothing 'Releasing File object
'MsgBox "1PreEnd of SUB check last_hostname_connected_value = " & last_hostname_connected_value
'last_hostname_connected_value=document.getElementById("savedhostname").value
'document.getElementById("savedhostname").value=last_hostname_connected_value
'MsgBox "2PreEnd of SUB check last_hostname_connected_value = " & last_hostname_connected_value
'document.getElementById("savedhostname").selected=david
'MsgBox "End of SUB check last_hostname_connected_value = " & last_hostname_connected_value
document.getElementById("IP-Input").value = last_IP_connected_value
'document.getElementById("IP-Input").value = last_hostname_connected_value
'document.getElementById("savedhostname").selected = last_hostname_connected_value
document.getElementById("savedhostname").value = last_IP_connected_value
End Sub
</script>
<style>
select > option[value=""] {
display: none;
}
</style>
</head>
<body>
TEST
<br>
IP: <input type='text' maxlength="15" class="enterhostip" size=22 id="IP-Input" name="hostname_IP" />
<br>
Saved Name : <select id="savedhostname" onchange="vbscript:changeNamechangeIP" ></select>
<br>
</body>
</html>
要在 select 中设置默认值,您可以通过将 selected
属性添加到特定的 <option>
来实现,例如
<select>
<option value=1>1</option>
<option value=2 selected>2</option>
</select>
因此,在您的循环中您将设置属性
Set objOption = document.createElement("OPTION")
objOption.Text = arrServiceList(0)
objOption.Value = arrServiceList(1)
if objOption.Value = last_IP_selected then
objOption.setAttribute "selected", "selected"
end if
这假定所有 objOption.Value
都是唯一的。
以下示例中有 3 个文件。 执行 dropdownboxtest.hta 显示 msgboxes 中所需的正确数据。 问题是下拉框:
IP 数据填写正确,下拉菜单填写正确(所有数据都存在)。问题是第一个选项是空白的,我试图用这个例子"colin","selected"选项使用代码:
document.getElementById("savedhostname").value = last_hostname_connected_value
或
document.getElementById("savedhostname").selected = last_hostname_connected_value
saved_last.cfg
last_hostname_connected = "colin"
last_IP_connected = "192.168.1.8"
savedhosts.txt
andy;192.168.1.5
brian;192.168.1.6
colin;192.168.1.8
david;192.168.1.15
eddie;192.168.1.54
dropdownboxtest.hta
<!DOCTYPE html>
<html>
<head>
<title>Test Dropdown Box</title>
<HTA:APPLICATION
ID="objHTA_Info"
APPLICATIONNAME="Test Dropdown Box"
SINGLEINSTANCE="no"
SCROLL="no"
BORDER="Thin"
BorderStyle="Raised"
Icon="#"
MaximizeButton="No"
>
<script language="javascript">
window.resizeTo (810, 634);
window.moveTo((screen.width - 10) / 2, (screen.height - 634) / 2);
</script>
<SCRIPT LANGUAGE="VBScript">
Dim last_hostname_connected_value, last_IP_connected_value
Sub Window_onLoad
With (CreateObject("Scripting.FileSystemObject"))
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
If .FileExists("saved_last.cfg") Then
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ReadTextFile = FSO.OpenTextFile("saved_last.cfg", 1) ' Open text with read only mode
Do Until ReadTextFile.AtEndOfStream
Textline = ReadTextFile.Readline()
If Instr(Textline, "last_hostname_connected =") Then
last_hostname_connected_value = Split(Textline, "=")(1)
last_hostname_connected_value = Replace(last_hostname_connected_value, Chr(34), "")
last_hostname_connected_value = LTrim(last_hostname_connected_value)
MsgBox "last_hostname_connected_value = " & last_hostname_connected_value
End If
If Instr(Textline, "last_IP_connected =") Then
last_IP_connected_value = Split(Textline, "=")(1)
last_IP_connected_value = Replace(last_IP_connected_value, Chr(34), "")
last_IP_connected_value = LTrim(last_IP_connected_value)
MsgBox "last_IP_connected_value : " & last_IP_connected_value
End If
Loop
End If
End With
Set obj = CreateObject("Scripting.FileSystemObject") 'Creating a File Object
Const ForReading = 1 'Defining Constant Value to read from a file
Set obj1 = obj.OpenTextFile("savedhosts.txt", ForReading) 'Opening a text file and reading text from it
Dim str,str1,objOption
'str=obj1.ReadAll 'All text from the file is read using ReadAll
'Msgbox str 'Contents of a file will be displayed through message box
'Do while obj1.AtEndofStream 'Reading text line wise using Do Loop and ReadLine
' str1=obj1.ReadLine
' Msgbox str1
'Loop
Call ClearHostnameListbox()
Do Until obj1.AtEndOfStream
strNextLine = obj1.ReadLine
arrServiceList = Split(strNextLine, ";")
'MsgBox "arrServiceList(0) = " & arrServiceList(0)
'MsgBox "arrServiceList(1) = " & arrServiceList(1)
Set objOption = document.createElement("OPTION")
objOption.Text = arrServiceList(0)
objOption.Value = arrServiceList(1)
savedhostname.Add(objOption)
'document.getElementById("savedhostname").value=77747
Loop
'MsgBox "arrServiceList(0) = " & arrServiceList(0)
'MsgBox "arrServiceList(1) = " & arrServiceList(1)
obj1.Close 'Closing a File
Set obj=Nothing 'Releasing File object
document.getElementById("savedhostname").value=last_hostname_connected_value
MsgBox "End of SUB check last_hostname_connected_value = " & last_hostname_connected_value
document.getElementById("IP-Input").value = last_IP_connected_value
End Sub
</script>
<SCRIPT LANGUAGE="VBScript">
Sub ClearHostnameListbox()
For Each objOption in savedhostname.Options
objOption.RemoveNode
Next
End Sub
</script>
</head>
<body>
TEST
<br>
IP: <input type='text' maxlength="15" class="enterhostip" size=22 id="IP-Input" name="hostname_IP" />
<br>
Saved Name : <select id="savedhostname" selected="savedhostname" maxlength="20" name="savedhostname" class="savedhostname" value=""></select>
</body>
</html>
显然这里发生了一些我似乎无法确定的事情,我们将不胜感激任何帮助。
这样试试:
<!DOCTYPE html>
<html>
<head>
<title>Test Dropdown Box</title>
<HTA:APPLICATION
ID="objHTA_Info"
APPLICATIONNAME="Test Dropdown Box"
SINGLEINSTANCE="no"
SCROLL="no"
BORDER="Thin"
BorderStyle="Raised"
Icon="#"
MaximizeButton="No"
>
<script language="javascript">
window.resizeTo (810, 634);
window.moveTo((screen.width - 10) / 2, (screen.height - 634) / 2);
</script>
<SCRIPT LANGUAGE="VBScript">
Dim last_hostname_connected_value, last_IP_connected_value
Sub Window_onLoad
With (CreateObject("Scripting.FileSystemObject"))
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
If .FileExists("saved_last.cfg") Then
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ReadTextFile = FSO.OpenTextFile("saved_last.cfg", 1) ' Open text with read only mode
Do Until ReadTextFile.AtEndOfStream
Textline = ReadTextFile.Readline()
If Instr(Textline, "last_hostname_connected =") Then
last_hostname_connected_value = Split(Textline, "=")(1)
last_hostname_connected_value = Replace(last_hostname_connected_value, Chr(34), "")
last_hostname_connected_value = LTrim(last_hostname_connected_value)
'MsgBox "last_hostname_connected_value = " & last_hostname_connected_value
End If
If Instr(Textline, "last_IP_connected =") Then
last_IP_connected_value = Split(Textline, "=")(1)
last_IP_connected_value = Replace(last_IP_connected_value, Chr(34), "")
last_IP_connected_value = LTrim(last_IP_connected_value)
'MsgBox "last_IP_connected_value : " & last_IP_connected_value
End If
Loop
End If
End With
Call ClearHostnameListbox()
Set obj = CreateObject("Scripting.FileSystemObject") 'Creating a File Object
Const ForReading = 1 'Defining Constant Value to read from a file
Set obj1 = obj.OpenTextFile("savedhosts.txt", ForReading) 'Opening a text file and reading text from it
Dim str,str1,objOption
'str=obj1.ReadAll 'All text from the file is read using ReadAll
'Msgbox str 'Contents of a file will be displayed through message box
'Do while obj1.AtEndofStream 'Reading text line wise using Do Loop and ReadLine
' str1=obj1.ReadLine
' Msgbox str1
'Loop
Do Until obj1.AtEndOfStream
strNextLine = obj1.ReadLine
arrServiceList = Split(strNextLine, ";")
'MsgBox "arrServiceList(0) = " & arrServiceList(0)
'MsgBox "arrServiceList(1) = " & arrServiceList(1)
Set objOption = document.createElement("OPTION")
objOption.Text = arrServiceList(0)
objOption.Value = arrServiceList(1)
savedhostname.Add(objOption)
'document.getElementById("savedhostname").value=77747
Loop
'MsgBox "arrServiceList(0) = " & arrServiceList(0)
'MsgBox "arrServiceList(1) = " & arrServiceList(1)
obj1.Close 'Closing a File
Set obj=Nothing 'Releasing File object
'document.getElementById("savedhostname").value=last_hostname_connected_value
'MsgBox "End of SUB check last_hostname_connected_value = " & last_hostname_connected_value
document.getElementById("IP-Input").value = last_IP_connected_value
End Sub
Sub ClearHostnameListbox()
For Each objOption in savedhostname.Options
objOption.RemoveNode
Next
End Sub
</script>
</head>
<body>
TEST
<br>
IP: <input type='text' maxlength="15" class="enterhostip" size=22 id="IP-Input" name="hostname_IP" />
<br>
Saved Name : <select id="savedhostname" selected="savedhostname" maxlength="20" name="savedhostname" class="savedhostname"></select>
</body>
</html>
以下乱七八糟的代码有效但由于一个原因而失败。
工作:
- 加载后,它使用最后已知的 Name/IP 并相应地填充 IP 文本和名称。
- 空行没了
- 从下拉框中选择另一个值会自动更改 IP 文本字段。
- IP 值已正确写入 "saved_last.cfg" 文件。
不工作:
- 它写入的主机名值 "saved_last.cfg" 不需要。 当您从下拉菜单中选择时,它会将 IP 地址写入 "last_hostname_connected_value" 而不是主机名。
<!DOCTYPE html>
<html>
<head>
<title>Test Dropdown Box</title>
<HTA:APPLICATION
ID="objHTA_Info"
APPLICATIONNAME="Test Dropdown Box"
SINGLEINSTANCE="no"
SCROLL="no"
BORDER="Thin"
BorderStyle="Raised"
Icon="#"
MaximizeButton="No"
>
<script language="javascript">
window.resizeTo (810, 634);
window.moveTo((screen.width - 10) / 2, (screen.height - 634) / 2);
</script>
<SCRIPT LANGUAGE="VBScript">
Dim last_hostname_connected_value, last_IP_connected_value
Sub Window_onLoad
'MsgBox "Start of SUB check last_hostname_connected_value = " & last_hostname_connected_value
With (CreateObject("Scripting.FileSystemObject"))
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
If .FileExists("saved_last.cfg") Then
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ReadTextFile = FSO.OpenTextFile("saved_last.cfg", 1) ' Open text with read only mode
Do Until ReadTextFile.AtEndOfStream
Textline = ReadTextFile.Readline()
If Instr(Textline, "last_hostname_connected =") Then
last_hostname_connected_value = Split(Textline, "=")(1)
last_hostname_connected_value = Replace(last_hostname_connected_value, Chr(34), "")
last_hostname_connected_value = LTrim(last_hostname_connected_value)
'MsgBox "last_hostname_connected_value = " & last_hostname_connected_value
End If
If Instr(Textline, "last_IP_connected =") Then
last_IP_connected_value = Split(Textline, "=")(1)
last_IP_connected_value = Replace(last_IP_connected_value, Chr(34), "")
last_IP_connected_value = LTrim(last_IP_connected_value)
'MsgBox "last_IP_connected_value : " & last_IP_connected_value
End If
Loop
End If
End With
Set obj = CreateObject("Scripting.FileSystemObject") 'Creating a File Object
Const ForReading = 1 'Defining Constant Value to read from a file
Set obj1 = obj.OpenTextFile("savedhosts.txt", ForReading) 'Opening a text file and reading text from it
Dim str,str1,objOption
'str=obj1.ReadAll 'All text from the file is read using ReadAll
'Msgbox str 'Contents of a file will be displayed through message box
'Do while obj1.AtEndofStream 'Reading text line wise using Do Loop and ReadLine
' str1=obj1.ReadLine
' Msgbox str1
'Loop
Call ClearHostnameListbox()
Do Until obj1.AtEndOfStream
strNextLine = obj1.ReadLine
arrServiceList = Split(strNextLine, ";")
'MsgBox "arrServiceList(0) = " & arrServiceList(0)
'MsgBox "arrServiceList(1) = " & arrServiceList(1)
Set objOption = document.createElement("OPTION")
objOption.Text = arrServiceList(0)
objOption.Value = arrServiceList(1)
OptionChooser = last_hostname_connected_value
savedhostname.Add(objOption)
'document.getElementById("savedhostname").select=77747
Loop
'MsgBox "arrServiceList(0) = " & arrServiceList(0)
'MsgBox "arrServiceList(1) = " & arrServiceList(1)
obj1.Close 'Closing a File
Set obj=Nothing 'Releasing File object
'MsgBox "1PreEnd of SUB check last_hostname_connected_value = " & last_hostname_connected_value
'last_hostname_connected_value=document.getElementById("savedhostname").value
'document.getElementById("savedhostname").value=last_hostname_connected_value
'MsgBox "2PreEnd of SUB check last_hostname_connected_value = " & last_hostname_connected_value
'document.getElementById("savedhostname").selected=david
'MsgBox "End of SUB check last_hostname_connected_value = " & last_hostname_connected_value
document.getElementById("IP-Input").value = last_IP_connected_value
'document.getElementById("IP-Input").value = last_hostname_connected_value
'document.getElementById("savedhostname").selected = last_hostname_connected_value
document.getElementById("savedhostname").value = last_IP_connected_value
End Sub
</script>
<SCRIPT LANGUAGE="VBScript">
Sub ClearHostnameListbox()
For Each objOption in savedhostname.Options
objOption.RemoveNode
Next
End Sub
Sub changeNamechangeIP
last_hostname_connected_value=document.getElementById("savedhostname").value
'MsgBox "last_hostname_connected_value = " & last_hostname_connected_value
last_IP_connected_value=document.getElementById("IP-Input").value
'MsgBox "last_IP_connected_value = " & last_IP_connected_value
Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Const ForWriting = 2
Dim myFile,myTemp,objFSO,objFile,strText,strNewText,objRegEx
Set myFile = objFSO.OpenTextFile("saved_last.cfg", ForReading, True)
Set myTemp= objFSO.OpenTextFile("saved_last.cfg" & ".tmp", ForWriting, True)
Do While Not myFile.AtEndofStream
myLine = myFile.ReadLine
If InStr(myLine, "last_IP_connected = ") Then
myLine = "last_IP_connected = " & Chr(34) & last_hostname_connected_value & Chr(34)
End If
If InStr(myLine, "last_hostname_connected = ") Then
myLine = "last_hostname_connected = " & Chr(34) & last_hostname_connected_value & Chr(34)
End If
myTemp.WriteLine myLine
Loop
myFile.Close
myTemp.Close
objFSO.DeleteFile("saved_last.cfg")
objFSO.MoveFile "saved_last.cfg" & ".tmp", "saved_last.cfg"
With (CreateObject("Scripting.FileSystemObject"))
'Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
If .FileExists("saved_last.cfg") Then
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ReadTextFile = FSO.OpenTextFile("saved_last.cfg", 1) ' Open text with read only mode
Do Until ReadTextFile.AtEndOfStream
Textline = ReadTextFile.Readline()
If Instr(Textline, "last_hostname_connected =") Then
last_hostname_connected_value = Split(Textline, "=")(1)
last_hostname_connected_value = Replace(last_hostname_connected_value, Chr(34), "")
last_hostname_connected_value = LTrim(last_hostname_connected_value)
'MsgBox "last_hostname_connected_value = " & last_hostname_connected_value
End If
If Instr(Textline, "last_IP_connected =") Then
last_IP_connected_value = Split(Textline, "=")(1)
last_IP_connected_value = Replace(last_IP_connected_value, Chr(34), "")
last_IP_connected_value = LTrim(last_IP_connected_value)
'MsgBox "last_IP_connected_value : " & last_IP_connected_value
End If
Loop
End If
End With
Set obj = CreateObject("Scripting.FileSystemObject") 'Creating a File Object
'Const ForReading = 1 'Defining Constant Value to read from a file
Set obj1 = obj.OpenTextFile("savedhosts.txt", ForReading) 'Opening a text file and reading text from it
Dim str,str1,objOption
'str=obj1.ReadAll 'All text from the file is read using ReadAll
'Msgbox str 'Contents of a file will be displayed through message box
'Do while obj1.AtEndofStream 'Reading text line wise using Do Loop and ReadLine
' str1=obj1.ReadLine
' Msgbox str1
'Loop
Call ClearHostnameListbox()
Do Until obj1.AtEndOfStream
strNextLine = obj1.ReadLine
arrServiceList = Split(strNextLine, ";")
'MsgBox "arrServiceList(0) = " & arrServiceList(0)
'MsgBox "arrServiceList(1) = " & arrServiceList(1)
Set objOption = document.createElement("OPTION")
objOption.Text = arrServiceList(0)
objOption.Value = arrServiceList(1)
OptionChooser = last_hostname_connected_value
savedhostname.Add(objOption)
'document.getElementById("savedhostname").select=77747
Loop
'MsgBox "arrServiceList(0) = " & arrServiceList(0)
'MsgBox "arrServiceList(1) = " & arrServiceList(1)
obj1.Close 'Closing a File
Set obj=Nothing 'Releasing File object
'MsgBox "1PreEnd of SUB check last_hostname_connected_value = " & last_hostname_connected_value
'last_hostname_connected_value=document.getElementById("savedhostname").value
'document.getElementById("savedhostname").value=last_hostname_connected_value
'MsgBox "2PreEnd of SUB check last_hostname_connected_value = " & last_hostname_connected_value
'document.getElementById("savedhostname").selected=david
'MsgBox "End of SUB check last_hostname_connected_value = " & last_hostname_connected_value
document.getElementById("IP-Input").value = last_IP_connected_value
'document.getElementById("IP-Input").value = last_hostname_connected_value
'document.getElementById("savedhostname").selected = last_hostname_connected_value
document.getElementById("savedhostname").value = last_IP_connected_value
End Sub
</script>
<style>
select > option[value=""] {
display: none;
}
</style>
</head>
<body>
TEST
<br>
IP: <input type='text' maxlength="15" class="enterhostip" size=22 id="IP-Input" name="hostname_IP" />
<br>
Saved Name : <select id="savedhostname" onchange="vbscript:changeNamechangeIP" ></select>
<br>
</body>
</html>
要在 select 中设置默认值,您可以通过将 selected
属性添加到特定的 <option>
来实现,例如
<select>
<option value=1>1</option>
<option value=2 selected>2</option>
</select>
因此,在您的循环中您将设置属性
Set objOption = document.createElement("OPTION")
objOption.Text = arrServiceList(0)
objOption.Value = arrServiceList(1)
if objOption.Value = last_IP_selected then
objOption.setAttribute "selected", "selected"
end if
这假定所有 objOption.Value
都是唯一的。