繁体   English   中英

在.hta文件中保留表单字段(输入/选择/复选框)。 因此原始用户和/或其他用户可以打开并查看填写的信息

[英]Preserving form fields(input/select/checkbox) in an .hta file. So original user and/or another user can open and see the filled out information

我需要为自己的工作创建的.hta文件提供帮助。 我对HTML很好,但是JavaScript对我来说是另一个领域。 我一直能够查看示例并将不同的示例组合在一起,但是我要完成的工作超出了我的专业知识。

现在,我已经成功创建了一个.hta ,允许用户选择一个复选框,告诉代码选择学校,然后在下拉列表中选择一个选项,并在文本字段中添加任何其他信息。 当用户单击“保存更改”时,它将输出一个.xml文件。

我的问题是,当用户关闭.hta文件时,重新打开时所有字段,选择和复选框都为空白。 我遍地搜索代码示例,试图找到一种保存数据的方法。 我不会详细介绍,但是html5 localstorage无效,JavaScript cookie无效,因为它们仅限于本地用户。

我想保留已输入的所有数据,以便任何用户(从任何工作站)都可以打开.hta ,而不必重新填写所有字段和选择。

我发现了三个解决方案:

  1. 外部序列化的外部JSON文件保存在本地,然后在打开.hta时调用。

  2. 使用这个家伙的JavaScript: hta-localstorage

  3. 从创建的.xml文件中读取信息。

不幸的是,我缺乏有关如何实现这些选项的知识。 如果我举一个现有代码的例子,有人可以帮助我实现前面提到的方法之一吗? 或者也许有人有一个更简单的选择?

脚本:

<script>
function WriteToFile()
{
  try 
  {
    var WshNetwork = new ActiveXObject("WScript.Network");
    var userName = WshNetwork.UserName;
    var fso, s;
    fso = new ActiveXObject("Scripting.FileSystemObject");
    s = fso.CreateTextFile("xml_output/sc_output.xml", true);

    var B_B_P=document.getElementById("A_Bright_Beginning_Preschool").checked;
    var B_B_P_selected=document.getElementById("A_Bright_Beginning_PreschoolSelected").value;   
    var B_B_P_other=document.getElementById("A_Bright_Beginning_PreschoolOther").value;

    var A_S_Cath_S=document.getElementById("All_Saints_Catholic_School").checked;
    var A_S_Cath_S_selected=document.getElementById("All_Saints_Catholic_SchoolSelected").value;    
    var A_S_Cath_S_other=document.getElementById("All_Saints_Catholic_SchoolOther").value;

s.writeline("\<\?xml version\=\"1\.0\" encoding\=\"UTF\-8\" standalone\=\"yes\"\?\>");
s.writeline("\<School\_data xmlns\:xsi\=\"http\:\/\/www\.w3\.org\/2001\/XMLSchema\-instance\"\>");

if (B_B_P==false)
        {
        s.writeline("");
        }
        else
            {
            s.writeline("   \<record\>");
            s.writeline("       \<School\_Name\>A Bright Beginning Preschool\<\/School\_Name\>");
            s.writeline("       \<School\_Seq\>001\<\/School\_Seq\>");
            s.writeline("       \<Delay\>" + B_B_P_selected + "\<\/Delay\>");
            s.writeline("       \<Other\>" + B_B_P_other + "\<\/Other\>");
            s.writeline("   \<\/record\>");
            }
if (A_S_Cath_S==false)
        {
        s.writeline("");
        }
        else
            {
            s.writeline("   \<record\>");
            s.writeline("       \<School\_Name\>All Saints Catholic School\<\/School\_Name\>");
            s.writeline("       \<School\_Seq\>002\<\/School\_Seq\>");
            s.writeline("       \<Delay\>" + A_S_Cath_S_selected + "\<\/Delay\>");
            s.writeline("       \<Other\>" + A_S_Cath_S_other + "\<\/Other\>");
            s.writeline("   \<\/record\>");
            }
            s.writeline("\<\/School\_data\>");
    s.Close();
  } 
  catch(err)
  {
   var strErr = 'Error:';
   strErr += '\nNumber:' + err.number;
   strErr += '\nDescription:' + err.description;
   document.write(strErr);
  }
}
</script>

HTML:

<h1>School Closures</h1>
<br />
<form>
<table width="900" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="left" valign="middle"><input type="checkbox" name="A_Bright_Beginning_Preschool" id="A_Bright_Beginning_Preschool" /></td>
    <td align="left" valign="middle">A Bright Beginning Preschool</td>
    <td align="left" valign="middle"><select name="A_Bright_Beginning_PreschoolSelected" id="A_Bright_Beginning_PreschoolSelected">
        <option value="" selected="selected"></option>
        <option value="1 Hour Late">1 Hour Late</option>
        <option value="2 Hours Late">2 Hours Late</option>
        <option value="3 Hours Late">3 Hours Late</option>
        <option value="4 Hours Late">4 Hours Late</option>
        <option value="No School">No School</option>
    </select></td>
    <td align="left" valign="middle"><input name="A_Bright_Beginning_PreschoolOther" type="text" id="A_Bright_Beginning_PreschoolOther" size="80" /></td>
  </tr>
  <tr>
    <td align="left" valign="middle"><input type="checkbox" name="All_Saints_Catholic_School" id="All_Saints_Catholic_School" /></td>
    <td align="left" valign="middle">All Saints Catholic School</td>
    <td align="left" valign="middle"><select name="All_Saints_Catholic_SchoolSelected" id="All_Saints_Catholic_SchoolSelected">
        <option value="" selected="selected"></option>
        <option value="1 Hour Late">1 Hour Late</option>
        <option value="2 Hours Late">2 Hours Late</option>
        <option value="3 Hours Late">3 Hours Late</option>
        <option value="4 Hours Late">4 Hours Late</option>
        <option value="No School">No School</option>
    </select></td>
    <td align="left" valign="middle"><input name="All_Saints_Catholic_SchoolOther" type="text" id="All_Saints_Catholic_SchoolOther" size="80" /></td>
  </tr>
</table>

您是否每次都在WriteToFile代码中创建XML文件?

s = fso.CreateTextFile(“ xml_output / sc_output.xml”,true);

调用时,它将覆盖先前创建的XML文件。 您将需要代码中的一些逻辑来检查XML文件是否存在,如果存在,请阅读相关内容并用它填充HTML字段。

用伪代码表示:

if not fso.fileExists("output.xml") then
Set outFILE = fso.createTextFile("output.xml")
else
Set inFILE = fso.openTextFile("output.xml"), 1) ' ForReading
rawXML = inFILE.ReadAll
inFILE.Close
end if

.. vbscript中有一些XML处理工具(创建XML对象并读取它,或处理特定元素等)。 但是您需要首先对文件创建问题进行排序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM