在 XSLT 中通过 onclick 事件传递变量无法正常工作

Passing variable via onclick event in XSLT doesn't work properly

本文关键字:常工作 工作 变量 XSLT onclick 事件      更新时间:2023-09-26

我是XSLT和XML的新手。我需要获取xml节点的值才能将其发送到Javascript。我尝试将值放入变量中并通过 onclick 事件发送变量。我很确定我在传递变量时做了一些可疑的事情。 请帮我弄清楚我在这里做错了什么!

<xsl:for-each select="itemCart/items">
         <xsl:if test="iQty>0">
          <tr>
            <xsl:variable name="myId" select="@iId" />
            <td><xsl:value-of select="iId"/></td>
            <td><xsl:value-of select="iName"/></td>
            <td><xsl:value-of select="iPrice"/></td>
            <td><xsl:value-of select="iQty"/></td>
            <td><xsl:value-of select="iDesc"/></td>
            <td><button type="button" onclick="addItem('myId')" >Add one to cart</button></td>
          </tr>
          </xsl:if>
          </xsl:for-each>
 1. When I alert it it outputs myId
 2. Without quotations it doesn't alert at all 

使用括号和$符号来访问变量,即:

<button type="button" onclick="addItem('{$myId}')">Add one to cart</button>