【发布时间】:2015-12-23 13:02:35
【问题描述】:
IDE:VS 2012
设计师代码
场景 1:使用 runat="server"(这是必需的),javascript 函数不起作用
<asp:TextBox ID="txtCost" runat="server" ReadOnly="true" Text="250/-"></asp:TextBox>
<input type="radio" name="myRadios" runat="server" id="rb1" checked="true" onchange="javascript:UpdateClick(this,'<%=txtCost.ClientID %>');" value="1" /> Monthly
<input type="radio" name="myRadios" runat="server" id="rb2" value="2" onchange="javascript:UpdateClick(this,'<%=txtCost.ClientID%>');" /> Weekly
场景 2:runat="server" 已移除(这是必需的),javascript 函数正常工作。
<asp:TextBox ID="txtCost" runat="server" ReadOnly="true" Text="250/-"></asp:TextBox>
<input type="radio" name="myRadios" id="rb1" checked="true" onchange="javascript:UpdateClick(this,'<%=txtCost.ClientID %>');" value="1" /> Monthly
<input type="radio" name="myRadios" id="rb2" value="2" onchange="javascript:UpdateClick(this,'<%=txtCost.ClientID%>');" /> Weekly
Javascript 函数:
function UpdateClick(myRadio, ClientID) {
currentValue = myRadio.value;
if (currentValue == '1') {
var txtCostBox = document.getElementById(ClientID);
txtCostBox.value = '250/-';
}
else {
var txtCostBox = document.getElementById(ClientID);
txtCostBox.value = '600/-';
}
return false;
}
问题:
因此,当我删除单选按钮中的 runat="server" 时,代码正在运行,而当我保留 runat="server" 时,我获取的不是实际的客户端 ID,而是客户端 ID:
'<%=txtCost.ClientID%>'
我有一个要求,我需要保留带有 runat="server" 的单选按钮,你能告诉我如何解决上述问题吗?
【问题讨论】:
-
这没有意义... runat=server 意味着您需要ClientID... 所以您说的似乎是相反的。
-
你甚至需要通过
ClientID的txtCost吗?它的 ID 看起来是静态的。此外还不清楚您的 js 是否根本没有运行,或者它只是没有收到ClientID。
标签: javascript c# jquery asp.net webforms