【发布时间】:2017-01-17 22:45:25
【问题描述】:
我正在使用 xsl 文件,该文件用于创建表单字段。我需要检查选项值是否与选定值相同,并将选定属性添加到该选项字段。这是创建选项的代码(这里一切正常,选项是动态创建的):
<xsl:when test="@type='select'">
<xsl:attribute name="title">Select List</xsl:attribute>
<xsl:apply-templates select=".">
<xsl:with-param name="field_id" select="$field_id" />
<xsl:with-param name="field_type" select="@type" />
<xsl:with-param name="isEditing" select="$isEditing" />
</xsl:apply-templates>
<xsl:element name="div">
<xsl:attribute name="id"><xsl:value-of select="$field_id" /></xsl:attribute>
<xsl:attribute name="type">select</xsl:attribute>
<xsl:attribute name="class">field ui-select</xsl:attribute>
<xsl:element name="select">
<xsl:attribute name="name"><xsl:value-of select="$field_id" /></xsl:attribute>
<xsl:attribute name="id"><xsl:value-of select="$field_id" /></xsl:attribute>
<xsl:attribute name="data-native-menu">false</xsl:attribute>
<xsl:attribute name="size">1</xsl:attribute>
<xsl:attribute name="required">
<xsl:choose>
<xsl:when test="@required='required'">required</xsl:when>
<xsl:otherwise>false</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:attribute name="onchange">CustomJS.manageDropdownSelect(this)</xsl:attribute>
<xsl:attribute name="data-value">
<xsl:choose>
<xsl:when test="@data-value!=''">
<xsl:value-of select="@data-value" /></xsl:when>
</xsl:choose>
</xsl:attribute>
<xsl:for-each select="./options/option">
<xsl:element name="option">
<xsl:attribute name="value">
<xsl:value-of select="." />
</xsl:attribute>
<xsl:value-of select="." />
<xsl:if test="@data-value='@value'">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:element>
<!-- field data -->
<xsl:element name="input">
<xsl:attribute name="id">field_id</xsl:attribute>
<xsl:attribute name="type">hidden</xsl:attribute>
<xsl:attribute name="class">fielddata</xsl:attribute>
<xsl:attribute name="value"><xsl:value-of select="concat($field_id,':',@type)" /></xsl:attribute>
</xsl:element>
</xsl:when>
这是添加data-value的部分:
<xsl:attribute name="data-value">
<xsl:choose>
<xsl:when test="@data-value!=''">
<xsl:value-of select="@data-value" /></xsl:when>
</xsl:choose>
</xsl:attribute>
这是负责选项的部分:
<xsl:for-each select="./options/option">
<xsl:element name="option">
<xsl:attribute name="value">
<xsl:value-of select="." />
</xsl:attribute>
<xsl:value-of select="." />
<xsl:if test="@data-value='@value'">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>
</xsl:element>
</xsl:for-each>
我需要弄清楚如何将此部分与所选值联系起来:
<xsl:if test="@data-value='@value'">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>
有什么想法吗?
编辑
我得到的 XML 代码示例:
<field type="select" name="" id="select_0" data-value="List 14" required="false">
<options>
<option>List 1</option>
<option>List 12</option>
<option>List 13</option>
<option>List 14</option>
<option>List 12</option>
<option>List 125</option>
</options>
</field>
【问题讨论】:
-
只是为了确认...您是说
<option>标签创建正确,但selected属性实际上并没有出现在其中的任何标签上?谢谢! -
是的,所有选项都创建没有任何问题,并且 select 正在正确获取数据值(基于所选选项)。