[英]How can I pass a label ID as a parameter to a javascript function and then chance the text of different labels as required
好吧,我也是javascript和sap.net的新手。 我一直在嘗試,但是認為代碼不正確。
<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>
在這里,onchange =“ GetCal(Label1)”將標簽ID作為參數傳遞,但是此代碼不會更改Label1上的文本。
這里document.getElementById('lbl')。innerHTML = data; 嘗試更改文本。 但是我想這些不是正確的方法,請幫助了解正確的語法。
希望我能夠將不同的標簽作為參數傳遞給該函數,以便我可以在需要時使用不同的標簽,並通過從DropDownList中選擇項目來更改標簽上的文本。 以及如何對許多DropDownList使用相同的功能。 假設我們有兩個DropDownList,並且我需要從DropDownList選擇項目時執行相同的任務,但是在這里var ddl = document.getElementById(“ <%= DropDownList1.ClientID%>”); 該函數僅是熟悉的DropDownList1。
那么,我需要進行哪些更改。
期待一些幫助,謝謝。
您可以簡單地使用“ 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.