admin管理员组

文章数量:1336438

Hopefully this one is not too hard to understand but I just want the label values to be inputs into a javascript function. Might be better to explain with code:

ASP.NET Label code:

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

Page Source:

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

What I would like to achieve but am not sure how to do:

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

So basically just wrap the output int in the following:

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

within the
<span></span>

Hopefully this one is not too hard to understand but I just want the label values to be inputs into a javascript function. Might be better to explain with code:

ASP.NET Label code:

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

Page Source:

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

What I would like to achieve but am not sure how to do:

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

So basically just wrap the output int in the following:

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

within the
<span></span>

Share asked Jul 9, 2010 at 22:48 Greg McNultyGreg McNulty 1,4665 gold badges28 silver badges50 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

Try this

<asp:Label ID="LabelCL" runat="server" />

<script type="text/javascript">
    var value = document.getElementById("<%= LabelCL.ClientID %>").innerHTML;
    functionX(value);
</script>

If it is called just after render you can simply use LabelCL.Text, if you need the value after and if it can be edited you can do as the exemple above.

With jQuery you can use

$("[id$=LabelCL]")

to retrieve the element.

var span = document.getElementById('ctl00_cpMainContent_LabelCL'),
    text = span.firstChild;

functionX(text.textContent);

Working Demo

or if defined in the Page use script tags to render out the client id for the span using

<%= LabelCL.ClientID %>

Try this:

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

You almost had it:

<asp:Label runat="server" Text="<script type='text/javascript'>document.write(functionX(7));</script>"/>

Use this piece of code

onclick="myFunction(labelName.innerText)"

本文标签: cHow do I get label value as a parameter into a javascript functionStack Overflow