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

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

我不知道如何做到这一点因为asp.net生成了RadioButton ID。

例如,我给出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>
有帮助吗?

解决方案

如果该代码在.aspx文件中,请使用<!> lt;%= MyRadioButton.ClientID%<!> gt;取而代之的是ASP.NET呈现引擎将为您正确呈现ID。

更新:例如:

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

其他提示

var radioButton = document.getElementById("<%= 1_RadioButton.ClientID %>");

然后从那里开始。注意 - 我对引号不肯。

我写了关于如何执行此操作的博文

  

所以说你有一个标签控件   你的页面:

     

     

生成的html和输出   该控件的ID可能看起来像   这样:

<span id="ctl00_ContentPlaceHolder1_Label1"></span>
  

不幸的是生成的ID   ct100_ContentPlaceHolder1_Label1不是   从构建开始总是一样的   建立。所以试着选择它   这样:

     

$("#ct100_ContentPlaceHolder1_Label1").hide();

     

最终会破裂,但不会   隐藏标签控件。

     

诀窍是在里面使用ASP.NET   jQuery选择器。 Label1.ClientID会   每次都返回生成的ID。我们   将ASP.NET和jQuery结合在一起   像这样的行:

     

$("#<%= Label1.ClientID %>").hide();

     

这将获得生成的ID   每次标签控制。

非常简单,只需将Web控件的clientidmode设置为静态(clientidmode="static"),您就可以确保asp.net将您的ID保留在设计器UI中。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top