在 HTA 中动态创建按钮会导致未定义的函数 onClick

Dynamically creating button in HTA causes undefined function onClick

我需要一个小型 HTA 来提供几个列表框和提交按钮,这将启动一些 SQL 位。当 HTML 部分是静态的时一切都很好,但是当我尝试使其动态化以便当前 month/year 可以在下拉菜单中默认时,代码停止工作并说我的 ButtonClick 未定义。 这是代码的简化版本。我试过只使用 btn01_OnClick、ButtonClick() 和其他一些没有结果的变体。想法?

<HEAD>
  <TITLE>Drop Down Menu</TITLE>
  <HTA:APPLICATION ID="oMyApp" 
    APPLICATIONNAME="Drop Down" 
    BORDER="Dialog"
    CAPTION="Yes"
    SCROLL="NO"
    SHOWINTASKBAR="yes"
    SINGLEINSTANCE="yes"
    SYSMENU="Yes"
    WINDOWSTATE="maximize">
</HEAD>

<SCRIPT LANGUAGE="VBScript">

Sub ButtonClick
        Document.write ("Success")
End Sub


Sub Window_OnLoad
strHTML = strHTML & "<BODY><SPAN>"
strHTML = strHTML & "<H2>Select Month</H2>"
strHTML = strHTML & "<P>Select Month: "
strHTML = strHTML & "<SELECT NAME=""Month"">"
    strHTML = strHTML & "<OPTION selected>" & MonthName(Month(Date),False) & "</OPTION>"
    strHTML = strHTML & "<OPTION>January</OPTION>"
strHTML = strHTML & "</SELECT><P>"

strHTML = strHTML & "<P>Select Year: "
strHTML = strHTML & "<SELECT NAME=""Year"">"
    strHTML = strHTML & "<OPTION selected>" & Year(Date) & "</OPTION>"
    strHTML = strHTML & "<OPTION>2014</OPTION>"
strHTML = strHTML & "</SELECT><P>"

strHTML = strHTML & "<BR><BR>"
strHTML = strHTML & "<Input Type = " & Chr(34) & "Button" & Chr(34) & " Name = " & Chr(34) & "btn01" & Chr(34) & " onClick = " & Chr(34) & "ButtonClick" & Chr(34) & " VALUE = " & Chr(34) & "SUBMIT" &  Chr(34) & ">"

strHTML = strHTML & "<BR><BR></SPAN>"
strHTML = strHTML & "</BODY>"
Document.write(strHTML)
    Window.Month.Focus
End Sub

</SCRIPT>

您的脚本部分应该是 <head> 部分的一部分,我会使用

Document.body.innerHtml = strHTML

而不是

Document.write strHTML

示例:

<html>
<head>
  <title>Drop Down Menu</title>
  <HTA:APPLICATION ID="oMyApp"
    ...
    WINDOWSTATE="maximize">

<script language="VBScript">
Sub ButtonClick
  Document.write "Success"
End Sub

Sub Window_OnLoad
  strHTML = "<SPAN>"
  ...
  strHTML = strHTML & "<BR><BR></SPAN>"
  Document.body.innerHtml = strHTML
  Window.Month.Focus
End Sub
</script>
</head>

<body>
</body>
</html>