javascript和asp.net网络用户控件 [英] javascript and asp.net web user controls

查看:93
本文介绍了javascript和asp.net网络用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web用户控件,可在客户端上生成以下html.

I have a web user control that generates the following html on the client.

我想使用JavaScript引用特定的RadioButton控件.

I want to reference a specific RadioButton control using JavaScript.

我不确定如何执行此操作,因为RadioButton ID是由asp.net生成的.

I am not sure how to do this as the RadioButton ID is generated by asp.net.

例如,我给RadioButton ID = 1_RadioButton

For example I give the RadioButton ID = 1_RadioButton

asp.net生成一个ID = ctl00_ContentPlaceHolder1_Material_1_RadioButton

asp.net generates an ID = ctl00_ContentPlaceHolder1_Material_1_RadioButton

此javascript代码如何找到单选按钮控件?

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>

以下是asp.net为客户端生成的内容.

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>

推荐答案

如果该代码位于.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.

更新:例如:

<script type="text/javascript">
    $(document).ready(function() {
        if ($get("<%= MyRadioButton.ClientID %>").checked) {
            $("#1_other").hide();
        }
    });    
</script>

这篇关于javascript和asp.net网络用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆