2016-03-26 14 views
0

複数のhtmlフォームを1つのvbscript関数に提出しようとしています。この関数の目的は、テキストファイルを更新することです。更新のために提出されたテキスト領域に応じて、関数が更新するテキストファイルを指定する必要があります。基本的には、すべてのテキスト領域のファイル更新を処理する関数が必要です。ここにコードスニペットがあります。複数のhtmlフォームをvbscript関数に送信

onload関数は、テキストファイルに現在あるテキストをテキスト領域に読み込みます。

F1およびF2機能は、テキスト領域をクリップボードにコピーします。基本的にはこれは単純なクイックリファレンスクリップボードです。

実際にどのようにこの概念をすべての機能に適用する必要があるかを見ることができます。複数のフォームがすべて単一の機能(読み取り、書き込み、コピー)に提出します。

<head> 
<title>Quick Comments</title> 
<HTA:APPLICATION 
    APPLICATIONNAME="Quick Comments" 
    SCROLL="YES" 
> 
</head> 
<script language="VBScript"> 
    sub Window_onLoad() 
     set oFSO=CreateObject("Scripting.FileSystemObject") 
     set oFile=oFSO.OpenTextFile("signature.txt",1) 
     text=oFile.ReadAll 
     document.all.var1.value=text 
     oFile.Close 
     set oFile=oFSO.OpenTextFile("initial.txt",1) 
     text=oFile.ReadAll 
     document.all.var2.value=text 
     oFile.Close 
    end sub 
    sub FileUpdate 
     Dim TheForm 
     Set TheForm = Document.field1 
     Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile("signature.txt",2,true) 
     objFileToWrite.WriteLine(TheForm.signature.value) 
     objFileToWrite.Close 
     Set objFileToWrite = Nothing 
    end sub 
    sub F1 
     Dim TheForm 
     Set TheForm = Document.field1 
     Set WshShell = CreateObject("WScript.Shell") 
     WshShell.Run "cmd.exe /c echo " & TheForm.signature.value & " | clip", 0, TRUE 
    end sub 
    sub F2 
     Dim TheForm 
     Set TheForm = Document.field2 
     Set WshShell = CreateObject("WScript.Shell") 
     WshShell.Run "cmd.exe /c echo " & TheForm.initial.value & " | clip", 0, TRUE 
    end sub 
</script> 
<body> 
    <h1>Quick Comments</h1><hr> 
    <form id="field1" name="field1"> 
     <textarea name="var1" id='signature' cols=75 rows=8></textarea> 
     <input type="button" name="copy" value="Copy" onclick="F1" language="vbscript"> 
     <input type="button" name="update" value="Update" onclick="FileUpdate" language="vbscript"> 
    </form> 
    <form id="field2" name="field2"> 
     <textarea name="var2" id='initial' cols=75 rows=8></textarea> 
     <input type="button" name="copy" value="Copy" onclick="F2" language="vbscript"> 
     <input type="button" name="copy" value="Copy" onclick="F1" language="vbscript"> 
    </form> 
</body> 

答えて

0

コードに基づいて以下の例を検討してください。読み書き操作のために別々のプロキシWriteTextFileReadTextFileがあります。また、window.clipboardData.SetDataクリップボードコントロールメソッドに切り替えました。私は、HTAと同じフォルダにあるテキストファイルは、そのフォルダへのパスがstrHtaFolder変数に格納されていると仮定します。ファイル名を更新するには、<textarea>タグ.nameプロパティを使用します。

<html> 
<head> 
    <title>Quick Comments</title> 
    <HTA:APPLICATION 
     ID="oHTA" 
     APPLICATIONNAME="Quick Comments" 
     SCROLL="YES" 
    > 
    </head> 
    <script language="VBScript"> 

     Dim strHtaFolder 

     Sub Window_onLoad() 
      strHtaFolder = CreateObject("Scripting.FileSystemObject").GetParentFolderName(Replace(oHTA.commandLine, """", "")) & "\" 
      document.all.signature.value = ReadTextFile(strHtaFolder & "signature.txt", -2) ' -2 - System default, -1 - Unicode, 0 - ASCII 
      document.all.initial.value = ReadTextFile(strHtaFolder & "initial.txt", -2) 
     End Sub 

     Sub FileUpdate() 
      Dim objForm, objTextArea 
      Set objForm = window.event.srcElement.parentNode 
      Set objTextArea = objForm.childNodes(0) 
      WriteTextFile objTextArea.value, strHtaFolder & objTextArea.name & ".txt", -2 
     End Sub 

     Sub CopyText() 
      Dim objForm, objTextArea 
      Set objForm = window.event.srcElement.parentNode 
      Set objTextArea = objForm.childNodes(0) 
      window.clipboardData.SetData "text", objTextArea.value 
     End Sub 

     Function ReadTextFile(strPath, lngFormat) 
      ' lngFormat -2 - System default, -1 - Unicode, 0 - ASCII 
      With CreateObject("Scripting.FileSystemObject").OpenTextFile(strPath, 1, False, lngFormat) 
       ReadTextFile = "" 
       If Not .AtEndOfStream Then ReadTextFile = .ReadAll 
       .Close 
      End With 
     End Function 

     Sub WriteTextFile(strContent, strPath, lngFormat) 
      ' lngFormat -2 - System default, -1 - Unicode, 0 - ASCII 
      With CreateObject("Scripting.FileSystemObject").OpenTextFile(strPath, 2, True, lngFormat) 
       .Write (strContent) 
       .Close 
      End With 
     End Sub 

    </script> 
    <body> 
     <h1>Quick Comments</h1><hr> 
     <form id="field1" name="field1"> 
      <textarea name="signature" id='signature' cols=75 rows=8></textarea> 
      <input type="button" name="copy" value="Copy" onclick="CopyText()" language="vbscript"> 
      <input type="button" name="update" value="Update" onclick="FileUpdate()" language="vbscript"> 
     </form> 
     <form id="field2" name="field2"> 
      <textarea name="initial" id='initial' cols=75 rows=8></textarea> 
      <input type="button" name="copy" value="Copy" onclick="CopyText()" language="vbscript"> 
      <input type="button" name="update" value="Update" onclick="FileUpdate()" language="vbscript"> 
     </form> 
    </body> 
</html> 

および第二の例では、わずかに変更された:

最初のものは現在のeventオブジェクトから必要<textarea>ノードを取得するためにwindow.event.srcElement.parentNode.childNodes(0)を使用します。これは、アレイにウィンドウ負荷署名と初期構成要素の両方に記憶し、ボタンが押されたときにイベントが発生したときに、後でそれだけサブへの配列の要素のインデックスを渡す:

BTW
<html> 
<head> 
    <title>Quick Comments</title> 
    <HTA:APPLICATION 
     ID="oHTA" 
     APPLICATIONNAME="Quick Comments" 
     SCROLL="YES" 
    > 
    </head> 
    <script language="VBScript"> 

     Dim strHtaFolder, arrItems 

     Sub Window_onLoad() 
      arrItems = Array(document.all.signature, document.all.initial) 
      strHtaFolder = CreateObject("Scripting.FileSystemObject").GetParentFolderName(Replace(oHTA.commandLine, """", "")) & "\" 
      arrItems(0).value = ReadTextFile(strHtaFolder & "signature.txt", -2) ' -2 - System default, -1 - Unicode, 0 - ASCII 
      arrItems(1).value = ReadTextFile(strHtaFolder & "initial.txt", -2) 
     End Sub 

     Sub FileUpdate(strItem) 
      WriteTextFile arrItems(strItem).value, strHtaFolder & arrItems(strItem).name & ".txt", -2 
     End Sub 

     Sub CopyText(strItem) 
      window.clipboardData.SetData "text", arrItems(strItem).value 
     End Sub 

     Function ReadTextFile(strPath, lngFormat) 
      ' lngFormat -2 - System default, -1 - Unicode, 0 - ASCII 
      With CreateObject("Scripting.FileSystemObject").OpenTextFile(strPath, 1, False, lngFormat) 
       ReadTextFile = "" 
       If Not .AtEndOfStream Then ReadTextFile = .ReadAll 
       .Close 
      End With 
     End Function 

     Sub WriteTextFile(strContent, strPath, lngFormat) 
      ' lngFormat -2 - System default, -1 - Unicode, 0 - ASCII 
      With CreateObject("Scripting.FileSystemObject").OpenTextFile(strPath, 2, True, lngFormat) 
       .Write (strContent) 
       .Close 
      End With 
     End Sub 

    </script> 
    <body> 
     <h1>Quick Comments</h1><hr> 
     <form id="field1" name="field1"> 
      <textarea name="signature" id='signature' cols=75 rows=8></textarea> 
      <input type="button" name="copy" value="Copy" onclick="CopyText(0)" language="vbscript"> 
      <input type="button" name="update" value="Update" onclick="FileUpdate(0)" language="vbscript"> 
     </form> 
     <form id="field2" name="field2"> 
      <textarea name="initial" id='initial' cols=75 rows=8></textarea> 
      <input type="button" name="copy" value="Copy" onclick="CopyText(1)" language="vbscript"> 
      <input type="button" name="update" value="Update" onclick="FileUpdate(1)" language="vbscript"> 
     </form> 
    </body> 
</html> 

<form>の使用タグが必要でない場合は、eを使用することができます。 g。代わりに<div>タグ。

関連する問題