javascript 和 asp.net Web 用户控件
我有一个 Web 用户控件,可以在客户端上生成以下 html。
我想使用 JavaScript 引用特定的 RadioButton 控件。
我不知道如何执行此操作,因为 RadioButton ID 是由 asp.net 生成的。
例如,我给RadioButton ID = 1_RadioButton
asp.net生成一个ID = ctl00_ContentPlaceHolder1_Material_1_RadioButton
这个javascript代码如何找到Radio Button控件?
<script type="text/javascript">
$(document).ready(function() {
if (document.getElementById("1_RadioButton").checked) {
$("#1_other").hide();
}
});
</script>
以下是 asp.net 为客户端生成的内容。
<input id="ctl00_ContentPlaceHolder1_Material_1_RadioButton" type="radio" name="ctl00$ContentPlaceHolder1$Material$Academic" value="1_RadioButton" onclick="javascript:$('#1_other').show('fast');" />
<label for="ctl00_ContentPlaceHolder1_Material_1_RadioButton">Yes</label>
<input id="ctl00_ContentPlaceHolder1_Material_2_RadioButton" type="radio" name="ctl00$ContentPlaceHolder1$Material$Academic" value="2_RadioButton" checked="checked" onclick="javascript:$('#1_other').hide('fast');" />
<label for="ctl00_ContentPlaceHolder1_Material_2_RadioButton">No</label>
<input id="ctl00_ContentPlaceHolder1_Material_3_RadioButton" type="radio" name="ctl00$ContentPlaceHolder1$Material$Academic" value="3_RadioButton" onclick="javascript:$('#1_other').hide('fast');" />
<label for="ctl00_ContentPlaceHolder1_Material_3_RadioButton">Uncertain</label>
<div id='1_other'>
<input name="ctl00$ContentPlaceHolder1$Material$4_TextBox" type="text" value="bbb chabng" id="ctl00_ContentPlaceHolder1_Material_4_TextBox" />
</div>
I have a web user control that generates the following html on the client.
I want to reference a specific RadioButton control using JavaScript.
I am not sure how to do this as the RadioButton ID is generated by asp.net.
For example I give the RadioButton ID = 1_RadioButton
asp.net generates an ID = ctl00_ContentPlaceHolder1_Material_1_RadioButton
how can this javascript code find Radio Button controls?
<script type="text/javascript">
$(document).ready(function() {
if (document.getElementById("1_RadioButton").checked) {
$("#1_other").hide();
}
});
</script>
The following is what asp.net generates for the client.
<input id="ctl00_ContentPlaceHolder1_Material_1_RadioButton" type="radio" name="ctl00$ContentPlaceHolder1$Material$Academic" value="1_RadioButton" onclick="javascript:$('#1_other').show('fast');" />
<label for="ctl00_ContentPlaceHolder1_Material_1_RadioButton">Yes</label>
<input id="ctl00_ContentPlaceHolder1_Material_2_RadioButton" type="radio" name="ctl00$ContentPlaceHolder1$Material$Academic" value="2_RadioButton" checked="checked" onclick="javascript:$('#1_other').hide('fast');" />
<label for="ctl00_ContentPlaceHolder1_Material_2_RadioButton">No</label>
<input id="ctl00_ContentPlaceHolder1_Material_3_RadioButton" type="radio" name="ctl00$ContentPlaceHolder1$Material$Academic" value="3_RadioButton" onclick="javascript:$('#1_other').hide('fast');" />
<label for="ctl00_ContentPlaceHolder1_Material_3_RadioButton">Uncertain</label>
<div id='1_other'>
<input name="ctl00$ContentPlaceHolder1$Material$4_TextBox" type="text" value="bbb chabng" id="ctl00_ContentPlaceHolder1_Material_4_TextBox" />
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果该代码位于 .aspx 文件中,请使用 <%= MyRadioButton.ClientID %> ASP.NET 渲染引擎将正确地为您渲染 ID。
更新: 例如:
If that code is in the .aspx file use <%= MyRadioButton.ClientID %> in its place and the ASP.NET rendering engine will properly render the ID for you.
Update: For example:
然后从那里开始。 注意 - 我对引号并不肯定。
And then go from there. Note - I'm not positive about the quotation marks.
我写了一篇关于如何执行此操作的博客文章:
I had written a blog post on how to do this:
非常简单,只需将 Web 控件的
clientidmode
设置为静态 (clientidmode="static"
),即可确定asp.net
将保留您在设计器 UI 中看到的ID
。Very simple just set the
clientidmode
of the web control to static (clientidmode="static"
) and you can be sure thatasp.net
will keep yourID
as seen in the designer UI.