代码之家  ›  专栏  ›  技术社区  ›  Greg McNulty

如何将标签值作为参数放入javascript函数中?

  •  0
  • Greg McNulty  · 技术社区  · 15 年前

    ASP.NET标签代码:

    <asp:Label ID="LabelCL" runat="server" Text="A single int filled in by DB"></asp:Label>
    

    页面来源:

    <span id="ctl00_cpMainContent_LabelCL">7</span>
    

    <span id="ctl00_cpMainContent_LabelCL">     <script type="text/javascript">functionX(7)</script>    </span> 
    

    所以基本上只需将输出int包装成以下形式:

    <script type="text/javascript">functionX( VALUE FROM LABEL TEXT)</script>
    


    <span></span>

    5 回复  |  直到 15 年前
        1
  •  3
  •   BrunoLM    15 年前

    试试这个

    <asp:Label ID="LabelCL" runat="server" />
    
    <script type="text/javascript">
        var value = document.getElementById("<%= LabelCL.ClientID %>").innerHTML;
        functionX(value);
    </script>
    

    如果在渲染之后调用它,您可以简单地使用 LabelCL.Text

    $("[id$=LabelCL]")
    

    检索元素。

        2
  •  2
  •   Russ Cam    15 年前
    var span = document.getElementById('ctl00_cpMainContent_LabelCL'),
        text = span.firstChild;
    
    functionX(text.textContent);
    

    Working Demo

    <%= LabelCL.ClientID %>
    
        3
  •  1
  •   Rubens Farias    15 年前

    试试这个:

    <asp:Label ID="LabelCL" runat="server" Text="A single int"></asp:Label>
    <button onclick="displayContent(<%= LabelCL.ClientID %>)">click me</button>
    <script>
        function displayContent(obj) {
            alert(obj.innerText);
        }
    </script>
    
        4
  •  1
  •   mbillard    15 年前

    你差点就成功了:

    <asp:Label runat="server" Text="<script type='text/javascript'>document.write(functionX(7));</script>"/>
    
        5
  •  0
  •   Hamza Zafeer Bert    9 年前

    使用这段代码

    onclick="myFunction(labelName.innerText)"