选择字段的粘滞表单
我想让我的表单在出现错误时保留提交时的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 Wendell Piez 方法:
注意:
$vMonths
声明只是考虑可读性使用简单递归:
两个输出:
编辑:至于@Flack 的评论,内联或外部源的经典答案:
With Wendell Piez method:
Note:
$vMonths
declaration just thinking in readabilityWith plain recursion:
Both output:
EDIT: As for @Flack's comment, the clasic answer with inline or external source: