简体   繁体   中英

How can I pass a label ID as a parameter to a javascript function and then chance the text of different labels as required

Well, I'm new to javascript and sap.net as well. I've been trying this but think the code isn't correct.

    <html>
<head runat="server">
<script type="text/javascript">
    function GetCal(lbl) 
    {
        var ddl = document.getElementById("<%=DropDownList1.ClientID%>");
        var SelVal = ddl.options[DropDownList1.selectedIndex].text;
        var a = parseInt("50");
        var data = a * SelVal;
        document.getElementById('lbl').innerHTML = data;

    }
    </script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" onchange="GetCal(Label1)">
        <asp:ListItem Text="Select" Value="0" />
                     <asp:ListItem Text="1"/>
                     <asp:ListItem Text="2"/>
                     <asp:ListItem Text="3"/>
                     <asp:ListItem Text="4"/>
                     <asp:ListItem Text="5"/>
        </asp:DropDownList>
        <asp:Label ID="Label1" runat="server" Text="text"></asp:Label>
        <asp:Label ID="Label2" runat="server" Text="text"></asp:Label>
    </div>
    </form>
</body>
</html>

Here onchange="GetCal(Label1)" pass the label Id as parametere but this code doesn't change the text on Label1.

here document.getElementById('lbl').innerHTML = data; try to change the text. But I guess these are not the proper way, please help to know right syntax.

Wishing that I would be able to pass different label as parametere to the function, so that I can use different labels when needed and does change the text on the label by selecting items from DropDownList. And how to use the same function for many DropDownLists that. Say we have two DropDownList and I need perform same task on selecting items from DropDownList but here var ddl = document.getElementById("<%=DropDownList1.ClientID%>"); the function is only familiar DropDownList1.

So, what are the changes I need to make.

Expecting some help thanks.

You can simply use "this" keyword as following:

<asp:DropDownList ID="DropDownList1" runat="server" onchange="GetCal(this);">
    <asp:ListItem Text="Select" Value="0" />
                 <asp:ListItem Text="1"/>
                 <asp:ListItem Text="2"/>
                 <asp:ListItem Text="3"/>
                 <asp:ListItem Text="4"/>
                 <asp:ListItem Text="5"/>
    </asp:DropDownList>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM