【问题标题】:xslt form fields - get input with unknown namexslt 表单字段 - 获取未知名称的输入
【发布时间】:2009-12-16 22:00:36
【问题描述】:

已更新 - 因为我的表达不清楚。会再试一次:

我有一个表单,其中包含多个动态创建的输入,如下所示:

<form id="tellafriend_form" method="post" action="landingpage.aspx">
  <!-- static example -->
  <input type="text" id="my_example" name="my_example" value="" />

  <!-- dynamic part -->
  <xsl:for-each select="something">
  <xsl:variable name="publicationId" select="@id"/>
    <input type="text" id="{$publicationId}" name="{$publicationId}" value="" />
  </xsl:for-each>
</form>

提交时如何使用 xslt 从输入中获取值?我可以从静态输入字段中获取它,但不能从动态字段中获取它,因为我不知道那里的名称/ID。

我知道所有 $publicationId 都是大于 2000 但小于 4000 的整数。如果需要,可以很容易地在它们前面加上一些文本(如果数字本身就有问题的话)。

首选 XSLT 解决方案。或者使用 jQuery 来解决问题(看到这个,这可能是另一种解决方案:Obtain form input fields using jQuery?)。

BR。安德斯

【问题讨论】:

  • 检索是什么意思?当用户在表单字段中输入值时,XSLT 已经呈现。
  • 对不起。烂英文。提交表单时,登录页面(form action="landing.aspx")必须从表单中获取值。但由于我不知道输入字段的名称/ID,我不知道如何在表单发布后从登录页面获取数据

标签: xslt forms input


【解决方案1】:

landingpage.aspx 将无法识别相关的 POST 值,因为您事先不知道输入元素名称。

这表明您需要使用一些您事先知道的附加数据来扩充名称。也就是说,在名称中添加额外信息,以便稍后检查。

一个不错的选择是以接收脚本能够(自动)将它们解释为数组的方式附加名称。这在接收脚本中更容易处理。一种这样的方式,取决于接收语言/框架允许的内容,是:

<form id="tellafriend_form" method="post" action="landingpage.aspx">
  <!-- static example -->
  <input type="text" id="my_example" name="my_example" value="" />

  <!-- dynamic part -->
  <xsl:for-each select="something">
  <xsl:variable name="publicationId" select="@id"/>
    <input type="text" id="{$publicationId}" name="publication[{$publicationId}]" value="" />
  </xsl:for-each>
</form>

试试这个并检查 POST 数据中的 publication 值。您可能会发现这是一个数组或哈希图。为此,就接收语言而言,您需要对表单中的数据数组使用正确的表示形式。

另一种选择是使用已知标识符扩充输入名称,然后在接收脚本中检查所有 POST 字段名称以获取相关标识符。例如:

<form id="tellafriend_form" method="post" action="landingpage.aspx">
  <!-- static example -->
  <input type="text" id="my_example" name="my_example" value="" />

  <!-- dynamic part -->
  <xsl:for-each select="something">
  <xsl:variable name="publicationId" select="@id"/>
    <input type="text" id="{$publicationId}" name="publication{$publicationId}" value="" />
  </xsl:for-each>
</form>


遍历所有收到的 POST name:value 对并检查名称以 'publication' 开头的那些。

为此,您必须选择一个不会出现在实际发布 ID 中的前置值。我在这里假设发布 ID 是数字,因此任何有意义的非数字前置值(例如“发布”)都是合适的。

【讨论】:

  • 超级!很好的解释。我可以这样做。我只是想到了另一种解决方案,如果 id 不是数字,则可以使用该解决方案:我根据给定规则创建动态输入(为 cat = 2 中的所有书籍创建输入)。如果着陆页知道 cat=2,那么它可以按照完全相同的规则提取数据(从基于 cat=2 中的书籍的名称中获取表单输入数据)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-29
  • 2018-03-14
  • 1970-01-01
  • 1970-01-01
  • 2018-01-23
  • 2015-04-13
相关资源
最近更新 更多