简体   繁体   English

如何将标签ID作为参数传递给javascript函数,然后根据需要输入不同标签的文本

[英]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. 好吧,我也是javascript和sap.net的新手。 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. 在这里,onchange =“ GetCal(Label1)”将标签ID作为参数传递,但是此代码不会更改Label1上的文本。

here document.getElementById('lbl').innerHTML = data; 这里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. 希望我能够将不同的标签作为参数传递给该函数,以便我可以在需要时使用不同的标签,并通过从DropDownList中选择项目来更改标签上的文本。 And how to use the same function for many DropDownLists that. 以及如何对许多DropDownList使用相同的功能。 Say we have two DropDownList and I need perform same task on selecting items from DropDownList but here var ddl = document.getElementById("<%=DropDownList1.ClientID%>"); 假设我们有两个DropDownList,并且我需要从DropDownList选择项目时执行相同的任务,但是在这里var ddl = document.getElementById(“ <%= DropDownList1.ClientID%>”); the function is only familiar DropDownList1. 该函数仅是熟悉的DropDownList1。

So, what are the changes I need to make. 那么,我需要进行哪些更改。

Expecting some help thanks. 期待一些帮助,谢谢。

You can simply use "this" keyword as following: 您可以简单地使用“ this”关键字,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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