2014-01-30 81 views
0

所以這裏是我想知道的男孩和女孩 - 首先我是一個完整的noob這個,只是剛剛開始,所以不要太努力我。我有一大堆的代碼我寫的,做一個.HTA應用:VBScript/HTA應用程序 - 一次運行多個程序用戶選擇

> <html> <head> 
> 
> <script type="text/vbscript"> 
> 
> Dim objShell Sub Button1_OnClick() 
> 
> if box2.checked AND box1.checked then 
> 
> Set objShell = CreateObject("WScript.Shell") 
> objShell.Run("""%programfiles(x86)%\Mozilla Firefox\firefox.exe""") 
> Set objShell = Nothing 
> 
> Set objShell = CreateObject("WScript.Shell") objShell.Run("cmd.exe") 
> Set objShell = Nothing 
> 
> elseif box1.checked then 
> 
> Set objShell = CreateObject("WScript.Shell") objShell.Run("cmd.exe") 
> Set objShell = Nothing 
> 
> Elseif box2.checked then 
> 
> Set objShell = CreateObject("WScript.Shell") 
> objShell.Run("""%programfiles(x86)%\Mozilla Firefox\firefox.exe""") 
> Set objShell = Nothing 
> 
> 
> End If End Sub 
> 
> 
> </script> </head> <body> <font face=Calibri> Check the program you 
> would like to run! <br> Available programs to run for now: <br> <input 
> type="checkbox" name="box1">CMD <br> <input type="checkbox" 
> name="box2">Mozilla <br> <i>Choose which program(s) you'd like to run. 
> It is possible to run multiple programs at one time!</i></font><br> 
> <input type="button" name="btn1" onclick="Button1_OnClick" 
> value="Submit"><br> <div id="error"></div> 
> 
> 
> 
> </body> </html> 

這完美的作品,因爲它應該是,當我檢查這兩個程序,他們都將運行,當我檢查他們中的一個,只有一個會運行。但是如果我在該列表上有50個不同的程序呢?我認爲寫一個比編寫每個程序組合的if/else/elseif語句加載更簡單的方法是嗎?如上所述,即時通訊完全禁止這樣做,也許我只是還沒有發現一種更簡單的方法呢......但這也是我問的原因。

回答

2

您可以將元素屬性用於您的目的。
迭代所有複選框,然後使用存儲在自定義屬性中的路徑啓動該過程(如果已選中)。完成。

<html> 
<head> 
    <script type="text/vbscript"> 
     Dim objShell 
     Set objShell = CreateObject("WScript.Shell") 

     Sub StartProcesses 
      Dim Checkbox 
      For Each Checkbox In Document.getElementsByName("process") 
       If Checkbox.Checked Then 
        objShell.Run """" & Checkbox.getAttribute("path") & """" 
       End If 
      Next 
     End Sub 
    </script> 
</head> 
<body> 
    <font face=Calibri> 
     Check the program you would like to run! <br> 
     Available programs to run for now: <br> 
     <div id="ProcessList"> 
      <input type="checkbox" name="process" path="cmd.exe">CMD <br> 
      <input type="checkbox" name="process" path="iexplore.exe">Internet Explorer <br> 
      <input type="checkbox" name="process" path="%programfiles(x86)%\Mozilla Firefox\firefox.exe">Firefox <br> 
      <input type="checkbox" name="process" path="calc.exe">Calculator <br> 
      <input type="checkbox" name="process" path="notepad.exe">Notepad <br> 
     </div> 
     <i>Choose which program(s) you'd like to run. It is possible to run multiple programs at one time!</i> 
    </font><br> 
    <input type="button" onclick="StartProcesses" value="Submit"><br> 
    <div id="error"></div> 
</body> 
</html> 
+0

你會如何添加一個MsgBox顯示警告時沒有複選框的檢查? – WatskeBart

+0

@WatskeBart看看https://gist.github.com/anonymous/e53b5fa495736ea6ba1dfa7dc32370b0 –

+0

知道了,非常感謝。 – WatskeBart