繁体   English   中英

XSL中单选按钮的每个循环

[英]For-each loop through radio buttons in XSL

我遇到以下问题:我有一个XML文件,正在使用XSL从中创建HTML文件。 在XSL文件中,我有一个for-each循环,它创建了几个单选按钮,当然我只需要检查一个按钮,但是在我的项目文件夹中打开index.php文件之后,可以检查所有单选按钮。 如何实现在循环后只能选择一个单选按钮? 先感谢您!

这里是循环内的代码片段:

<div data-role="fieldcontain">
    <fieldset data-role="controlgroup">
        <input type="radio" name="radio-choice" id="radio-choice-3"  />
        <label for="radio-choice-3"><xsl:value-of select="text"/></label>
    </fieldset>
</div>

PS:我想这是ID不变的,每次循环执行代码时,都会创建一个独立的单选按钮,并且显然属性controlgroup组不会将这些按钮分组。

此转换

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="x">
    <div data-role="fieldcontain">
        <fieldset data-role="controlgroup">
            <input type="radio" name="radio-choice" id="radio-choice-{position()}"  />
            <label for="radio-choice-{position()}"><xsl:value-of select="text"/></label>
        </fieldset>
    </div>
 </xsl:template>
</xsl:stylesheet>

当应用于以下XML文档时 (未提供任何信息!!):

<t>
 <x>
  <text>Choice one</text>
 </x>
 <x>
  <text>Choice two</text>
 </x>
 <x>
  <text>Choice three</text>
 </x>
</t>

产生想要的正确结果:

<div data-role="fieldcontain">
   <fieldset data-role="controlgroup">
      <input type="radio" name="radio-choice" id="radio-choice-1"/>
      <label for="radio-choice-1">Choice one</label>
   </fieldset>
</div>
<div data-role="fieldcontain">
   <fieldset data-role="controlgroup">
      <input type="radio" name="radio-choice" id="radio-choice-2"/>
      <label for="radio-choice-2">Choice two</label>
   </fieldset>
</div>
<div data-role="fieldcontain">
   <fieldset data-role="controlgroup">
      <input type="radio" name="radio-choice" id="radio-choice-3"/>
      <label for="radio-choice-3">Choice three</label>
   </fieldset>
</div>

并且在浏览器中显示时,任何时候都只能将一个单选按钮置于选中状态。

为了仅检查一个单选按钮,所有<input type="radio"/>元素必须具有相同的@name属性。 例如:

<input type="radio" name="radio-choice" id="radio-choice-3"/>
<input type="radio" name="radio-choice" id="radio-choice-4"/>

暂无
暂无

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

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