如何从动态生成的按钮执行代码

时间:2014-09-10 06:37:43

标签: button user-interface hta printers

我尝试制作一个gui(hta),它可以安装在客户端,打印机服务器上的一台或多台打印机上。

问题是当我创建按钮" install"时,该功能在表单加载时执行,而不是单击按钮。

我不明白为什么。你可以帮帮我吗?

<HTML>
<HEAD> 
    <TITLE>printer installation</TITLE>
    <HTA:APPLICATION ID = 'AppBase'>

        <script language="VBScript">



    Dim WshNetwork, objPrinter, intDrive, intNetLetter
    strComputer = "change_printer_server_name_or_ip" 

    strHTML =  "<TABLE BORDER='0'>"

    Set WshNetwork = CreateObject("WScript.Network") 
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
    Set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer") 
    For Each objPrinter in colInstalledPrinters 

        strHTML = strHTML & "<TR><TD><input type='button' value='install printer' name='" & objPrinter.Name & "' onclick='" & installprinter(printname, printserver) &"'>" & objPrinter.Name & "</input></TD><TD>" & objPrinter.Location  & "</TD><TD>" & objPrinter.portname & "</TD></TR>"

    next
    strHTML = strHTML & "</TABLE>"

    DataArea.InnerHTML = strHTML

    function installprinter(printname, printserver)
        Set WshNetwork = CreateObject ("WScript.Network")
        on error resume next
        PrinterPath = "\\" & printserver & "\" & printname
        WshNetwork.RemovePrinterConnection PrinterPath, true, true
        WshNetwork.AddwindowsPrinterConnection (PrinterPath)
        msgBox "L'imprimante a été installée avec succès"
    end function


    </script>
</HEAD>
<BODY>

     <span id="DataArea"></span>



</BODY>

2 个答案:

答案 0 :(得分:0)

您网页的加载顺序非常重要。

当您尝试分配DataArea时,DOM尚未初始化,因此DataArea不存在。以下是在DOM准备就绪时如何调用代码的简单示例:

<html>
<head> 
    <title>printer installation</title>
    <HTA:APPLICATION ID = 'AppBase'>
    <script language="VBScript">
    strHTML =  "<TABLE BORDER='0'>"
    strHTML = strHTML & "<tr><td>Testing</td></tr>"
    strHTML = strHTML & "</TABLE>"

    Function OnLoad()
        DataArea.InnerHTML = strHTML
    End Function
    </script>
</head>
<body onload="OnLoad">
     <span id="DataArea"></span>
</body>
</html>

请注意<body onload="OnLoad">和新Function OnLoad()

编辑 - 更新为包含从生成的html调用的函数:

<html>
<head> 
    <title>printer installation</title>
    <HTA:APPLICATION ID = 'AppBase'>
    <script language="VBScript">
    strHTML =  "<TABLE BORDER='0'>"
    strHTML = strHTML & "<tr><td><button onclick=""TestingFunction"">Testing</button></td></tr>"
    strHTML = strHTML & "</TABLE>"

    Function OnLoad()
        DataArea.InnerHTML = strHTML
    End Function

    Function TestingFunction()
        MsgBox("Hello")
    End Function
    </script>
</head>
<body onload="OnLoad">
     <span id="DataArea"></span>
</body>
</html>

答案 1 :(得分:0)

生成HTML时,您实际上正在调用 installprinter 例程。相反,您需要在单击按钮时将HTML格式化为有效调用。您希望最终的HTML看起来像这样:

<TR><TD><input type='button' value='install printer' name='PrinterName'
    onclick='installprinter "PrinterName", "PCName"'>PrinterName</input></TD>
    <TD>PrinterLoc</TD>
    <TD>PortName</TD></TR>

请注意,由于您正在调用sub,因此请排除括号。它看起来很奇怪,但这就是VBScript的工作方式。因此,生成表格行的代码应该是这样的:

strHTML = strHTML & "<TR><TD><input type='button' value='install printer' name='" & _
    objPrinter.Name & "' onclick='installprinter """ & objPrinter.Name & """, """ & _
    strComputer & """'>" & objPrinter.Name & "</input></TD><TD>" & _
    objPrinter.Location  & "</TD><TD>" & objPrinter.portname & "</TD></TR>"