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