选择字段的粘滞表单

发布于 2024-10-24 16:05:18 字数 1217 浏览 1 评论 0原文

我想让我的表单在出现错误时保留提交时的 smae 值。您可以查看我为选项“3”创建的示例。我不想为每个选择字段上的每个选项编写相同的代码块。一定有更好的方法。想法?谢谢SO们!

<select id="card-expires-month" name="fields[card-expires-month]">
    <option value="1" selected="selected">1 - January</option>
    <option value="2">2 - February</option>

    <xsl:element name="option">

        <xsl:attribute name="value">3</xsl:attribute>

        <xsl:if test="//data/events/deposit/post-values/card-expires-month = 3">
            <xsl:attribute name="selected">selected</xsl:attribute>
        </xsl:if>       

        <xsl:text>3 - March</xsl:text>

    </xsl:element>

    <option value="4">4 - April</option>
    <option value="5">5 - May</option>
    <option value="6">6 - June</option>
    <option value="7">7 - July</option>
    <option value="8">8 - August</option>
    <option value="9">9 - September</option>
    <option value="10">10 - October</option>
    <option value="11">11 - November</option>
    <option value="12">12 - December</option>
</select>

I want to make my forms keep the smae value as submitted when there's an error. you can see the example i've created for option "3". I dont want to write this same chunk of code for every option on every select field. There must be a better way. Thoughts? Thanks SO'ers!

<select id="card-expires-month" name="fields[card-expires-month]">
    <option value="1" selected="selected">1 - January</option>
    <option value="2">2 - February</option>

    <xsl:element name="option">

        <xsl:attribute name="value">3</xsl:attribute>

        <xsl:if test="//data/events/deposit/post-values/card-expires-month = 3">
            <xsl:attribute name="selected">selected</xsl:attribute>
        </xsl:if>       

        <xsl:text>3 - March</xsl:text>

    </xsl:element>

    <option value="4">4 - April</option>
    <option value="5">5 - May</option>
    <option value="6">6 - June</option>
    <option value="7">7 - July</option>
    <option value="8">8 - August</option>
    <option value="9">9 - September</option>
    <option value="10">10 - October</option>
    <option value="11">11 - November</option>
    <option value="12">12 - December</option>
</select>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空‖城人不在 2024-10-31 16:05:18

使用 Wendell Piez 方法:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <xsl:variable name="vMonths">
            <xsl:text>1 - January|2 - February|3 - March|</xsl:text>
            <xsl:text>4 - April|5 - May|6 - June|</xsl:text>
            <xsl:text>7 - July|8 - August|9 - September|</xsl:text>
            <xsl:text>10 - October|11 - November|12 - December|</xsl:text>
        </xsl:variable>
        <select id="card-expires-month" name="fields[card-expires-month]">
            <xsl:for-each select="(//node())[12 >= position()]">
                <option value="{position()}">
                    <xsl:if test="//data
                                    /events
                                      /deposit
                                        /post-values
                                          /card-expires-month
                                  = position()">
                        <xsl:attribute name="selected">selected</xsl:attribute>
                    </xsl:if>
                    <xsl:value-of select="concat(
                                         position(),
                                         substring-before(
                                            substring-after(
                                               $vMonths,
                                               position()
                                            ),
                                            '|'
                                         )
                                      )"/>
                </option>
            </xsl:for-each>
        </select>
    </xsl:template>
</xsl:stylesheet>

注意$vMonths 声明只是考虑可读性

使用简单递归:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <select id="card-expires-month" name="fields[card-expires-month]">
            <xsl:call-template name="tokenize"/>
        </select>
    </xsl:template>
    <xsl:template name="tokenize">
        <xsl:param name="pString">
            <xsl:text>January|February|March|April|</xsl:text>
            <xsl:text>May|June|July|August|</xsl:text>
            <xsl:text>September|October|November|December</xsl:text>
        </xsl:param>
        <xsl:param name="pPosition" select="1"/>
        <xsl:choose>
            <xsl:when test="$pString=''"/>
            <xsl:when test="contains($pString,'|')">
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="pString"
                                    select="substring-before($pString,'|')"/>
                    <xsl:with-param name="pPosition" select="$pPosition"/>
                </xsl:call-template>
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="pString"
                                    select="substring-after($pString,'|')"/>
                    <xsl:with-param name="pPosition" select="$pPosition + 1"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <option value="{$pPosition}">
                    <xsl:if test="//data
                                    /events
                                      /deposit
                                        /post-values
                                          /card-expires-month
                                  = $pPosition">
                        <xsl:attribute name="selected">selected</xsl:attribute>
                    </xsl:if>
                    <xsl:value-of select="concat($pPosition,' - ',$pString)"/>
                </option>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

两个输出:

<select id="card-expires-month" name="fields[card-expires-month]">
    <option value="1">1 - January</option>
    <option value="2">2 - February</option>
    <option value="3">3 - March</option>
    <option value="4">4 - April</option>
    <option value="5">5 - May</option>
    <option value="6">6 - June</option>
    <option value="7">7 - July</option>
    <option value="8">8 - August</option>
    <option value="9">9 - September</option>
    <option value="10">10 - October</option>
    <option value="11">11 - November</option>
    <option value="12">12 - December</option>
</select>

编辑:至于@Flack 的评论,内联或外部源的经典答案:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:m="month"
 exclude-result-prefixes="m">
    <xsl:output method="html"/>
    <m:m>January</m:m>
    <m:m>February</m:m>
    <m:m>March</m:m>
    <m:m>April</m:m>
    <m:m>May</m:m>
    <m:m>June</m:m>
    <m:m>July</m:m>
    <m:m>August</m:m>
    <m:m>September</m:m>
    <m:m>October</m:m>
    <m:m>November</m:m>
    <m:m>December</m:m>
    <xsl:template match="/">
        <xsl:variable name="vSelected"
         select="//data/events/deposit/post-values/card-expires-month"/>
        <select id="card-expires-month" name="fields[card-expires-month]">
            <xsl:for-each select="document('')/*/m:m">
                <option value="{position()}">
                    <xsl:if test="$vSelected = position()">
                        <xsl:attribute name="selected">selected</xsl:attribute>
                    </xsl:if>
                    <xsl:value-of select="concat(position(),' - ',.)"/>
                </option>
            </xsl:for-each>
        </select>
    </xsl:template>
</xsl:stylesheet>

With Wendell Piez method:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <xsl:variable name="vMonths">
            <xsl:text>1 - January|2 - February|3 - March|</xsl:text>
            <xsl:text>4 - April|5 - May|6 - June|</xsl:text>
            <xsl:text>7 - July|8 - August|9 - September|</xsl:text>
            <xsl:text>10 - October|11 - November|12 - December|</xsl:text>
        </xsl:variable>
        <select id="card-expires-month" name="fields[card-expires-month]">
            <xsl:for-each select="(//node())[12 >= position()]">
                <option value="{position()}">
                    <xsl:if test="//data
                                    /events
                                      /deposit
                                        /post-values
                                          /card-expires-month
                                  = position()">
                        <xsl:attribute name="selected">selected</xsl:attribute>
                    </xsl:if>
                    <xsl:value-of select="concat(
                                         position(),
                                         substring-before(
                                            substring-after(
                                               $vMonths,
                                               position()
                                            ),
                                            '|'
                                         )
                                      )"/>
                </option>
            </xsl:for-each>
        </select>
    </xsl:template>
</xsl:stylesheet>

Note: $vMonths declaration just thinking in readability

With plain recursion:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <select id="card-expires-month" name="fields[card-expires-month]">
            <xsl:call-template name="tokenize"/>
        </select>
    </xsl:template>
    <xsl:template name="tokenize">
        <xsl:param name="pString">
            <xsl:text>January|February|March|April|</xsl:text>
            <xsl:text>May|June|July|August|</xsl:text>
            <xsl:text>September|October|November|December</xsl:text>
        </xsl:param>
        <xsl:param name="pPosition" select="1"/>
        <xsl:choose>
            <xsl:when test="$pString=''"/>
            <xsl:when test="contains($pString,'|')">
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="pString"
                                    select="substring-before($pString,'|')"/>
                    <xsl:with-param name="pPosition" select="$pPosition"/>
                </xsl:call-template>
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="pString"
                                    select="substring-after($pString,'|')"/>
                    <xsl:with-param name="pPosition" select="$pPosition + 1"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <option value="{$pPosition}">
                    <xsl:if test="//data
                                    /events
                                      /deposit
                                        /post-values
                                          /card-expires-month
                                  = $pPosition">
                        <xsl:attribute name="selected">selected</xsl:attribute>
                    </xsl:if>
                    <xsl:value-of select="concat($pPosition,' - ',$pString)"/>
                </option>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

Both output:

<select id="card-expires-month" name="fields[card-expires-month]">
    <option value="1">1 - January</option>
    <option value="2">2 - February</option>
    <option value="3">3 - March</option>
    <option value="4">4 - April</option>
    <option value="5">5 - May</option>
    <option value="6">6 - June</option>
    <option value="7">7 - July</option>
    <option value="8">8 - August</option>
    <option value="9">9 - September</option>
    <option value="10">10 - October</option>
    <option value="11">11 - November</option>
    <option value="12">12 - December</option>
</select>

EDIT: As for @Flack's comment, the clasic answer with inline or external source:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:m="month"
 exclude-result-prefixes="m">
    <xsl:output method="html"/>
    <m:m>January</m:m>
    <m:m>February</m:m>
    <m:m>March</m:m>
    <m:m>April</m:m>
    <m:m>May</m:m>
    <m:m>June</m:m>
    <m:m>July</m:m>
    <m:m>August</m:m>
    <m:m>September</m:m>
    <m:m>October</m:m>
    <m:m>November</m:m>
    <m:m>December</m:m>
    <xsl:template match="/">
        <xsl:variable name="vSelected"
         select="//data/events/deposit/post-values/card-expires-month"/>
        <select id="card-expires-month" name="fields[card-expires-month]">
            <xsl:for-each select="document('')/*/m:m">
                <option value="{position()}">
                    <xsl:if test="$vSelected = position()">
                        <xsl:attribute name="selected">selected</xsl:attribute>
                    </xsl:if>
                    <xsl:value-of select="concat(position(),' - ',.)"/>
                </option>
            </xsl:for-each>
        </select>
    </xsl:template>
</xsl:stylesheet>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文