当我使用下面的代码时,我可以获得要更改的标签值
document.getElementById('<%=lblDropdownValue.ClientID %>').innerHTML = ddl.value;
但我想将它作为参数传递如下所示,但它似乎不起作用。
<table>
<tbody>
<tr>
<td>
<asp:DropDownList id="ddlProducts" runat="server"
onclick="myfunction(this,'<%=lblDropdownValue.ClientID %> "')">
<asp:ListItem Selected="True" Value="-1"> Please Select </asp:ListItem>
<asp:ListItem Value="Car"> BMW </asp:ListItem>
<asp:ListItem Value="Music"> MP3 </asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:Label Text="" id="lblDropdownValue" runat="server"/>
</td><td></td>
</tr>
</tbody>
</table>
</div>
</form>
<script>
function myfunction(ddl, lblText)
{
document.getElementById("'"+lblText+"'").innerHTML = ddl.value;
}
</script>
最佳答案
把这个放在后面的代码中:
public void Page_Load( ... ) {
ddlProducts.Attributes["onclick"] = "myfunction(...)";
}
并从 ASPX 中删除 onclick
。
这样做的原因是您希望在客户端附加 onclick
。您现在拥有的是一个在服务器端评估的属性,因此它无法正确地提供给客户端。
关于javascript - 将 ASP.NET 标签 ID 传递给 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28465305/