简体   繁体   English

XSL中单选按钮的每个循环

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

I have got the following problem: I have an XML file, from which I am creating an HTML file using XSL. 我遇到以下问题:我有一个XML文件,正在使用XSL从中创建HTML文件。 In the XSL file I have a for-each loop, which creates a couple of radio buttons and of course I need the only one button being checked, but after I open my index.php file in my project folder it is possible to check ALL radio buttons. 在XSL文件中,我有一个for-each循环,它创建了几个单选按钮,当然我只需要检查一个按钮,但是在我的项目文件夹中打开index.php文件之后,可以检查所有单选按钮。 How can I achieve, that after looping only one radio button can be selected? 如何实现在循环后只能选择一个单选按钮? Thank you in advance! 先感谢您!

Here the code snippet inside the loop: 这里是循环内的代码片段:

<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 I suppose it is the ID that does not change and every time a loop goes through the code an independent radio button is created and obviously the attribute controlgroup doesn't group the buttons. PS:我想这是ID不变的,每次循环执行代码时,都会创建一个独立的单选按钮,并且显然属性controlgroup组不会将这些按钮分组。

This transformation : 此转换

<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>

when applied on the following XML document (none has been provided !!!): 当应用于以下XML文档时 (未提供任何信息!!):

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

produces the wanted, correct result: 产生想要的正确结果:

<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>

and when displayed in the browser, only one radio button can be in selected state at any time. 并且在浏览器中显示时,任何时候都只能将一个单选按钮置于选中状态。

To make it possible to check only one radio button, all of the <input type="radio"/> elements must have the same @name attribute. 为了仅检查一个单选按钮,所有<input type="radio"/>元素必须具有相同的@name属性。 For example: 例如:

<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