添加runat属性时,aspx页面的javascript中无法访问html复选框 [英] Html checkbox is not accessible in javascript of aspx page when adding runat attribute

查看:63
本文介绍了添加runat属性时,aspx页面的javascript中无法访问html复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请紧急帮助我

我是
制作一个dotnet页面,我将使用4 html复选框,并显示每个复选框的onclick并隐藏一个div框.我做一个功能check1并调用html复选框.但是,当我想在aspx.cs C#页面上的此复选框进行编码时,我在每个html复选框中添加了runat ="server"属性.之后,javascript给出了单击复选框时的错误,这是预期的对象.请帮帮我.

代码如下:-

Please Help me It''s Urgent

I am
Making a dotnet page where i am take 4 html checkbox and onclick of every checkbox i display and hide a div box. i am make a function check1 and call the html checkbox. But when i want to this checkbox on aspx.cs C# page for coding so i am adding runat="server" attribute in every html checkbox. After that javascript gives error on click of checkbox is object expected. Please help me.

code is below: --

    function check1() {
			alert("hi");
//        alert(document.getElementById(''add_acad''));
        var checkbox1 = document.getElementById(''add_acad'');
        var panel1=document.getElementById(''academicpanel'');
        if (checkbox1.checked)
            panel1.style.display = ''block'';
        else
            panel1.style.display = ''none'';
        var checkbox2 = document.getElementById(''add_grade'');
        var panel2 = document.getElementById(''gradepanel'');
        if (checkbox2.checked) {
            panel2.style.display = ''block'';
        }
        else
            panel2.style.display = ''none'';
        var checkbox3 = document.getElementById(''add_health'');
        var panel3 = document.getElementById(''healthpanel'');
        if (checkbox3.checked) {
            panel3.style.display = ''block'';
        }
        else
            panel3.style.display = ''none'';
        var checkbox4 = document.getElementById(''addother'');
        var panel4 = document.getElementById(''leavepanel'');
        if (checkbox4.checked)
            panel4.style.display = ''block'';
        else
            panel4.style.display = ''none'';
    }


 <table width="100%" border="1" cellspacing="0" cellpadding="0">
        <tr>
          <td><label>
            <input type="checkbox"  runat="server" name="add_acad" id="add_acad" checked="checked"  önclick="check1()"/>
          Add Academic Record</label></td>
          <td><label>
            <input type="checkbox"  runat="server" name="add_grade" id="add_grade"   önclick="check1()"/>
          Add Grade</label></td>
          <td><label>
            <input type="checkbox"  runat="server" name="add_health" id="add_health"   önclick="check1()"/>
          Add Health Record</label></td>
          <td><label>
            <input type="checkbox"  runat="server" name="addother" id="addother"   önclick="check1()"/>
          Add Other Record</label></td>
        </tr>
      </table>

推荐答案

ASP.NET为控件添加了前缀,例如"ctl00_PlaceHolderArea",因此当您尝试按原样访问JavaScript时...

ASP.NET addes a prefix to controls, something like "ctl00_PlaceHolderArea", so when trying to access the is JavaScript as you are...

document.getElementById(''add_acad'');



...该控件不再具有相同的名称.现在就像这样



...the control no longer has the same name. It would now be something like this

document.getElementById(''ctl00_PlaceHolderArea_add_acad'');



您应该研究使用JQuery.您可以将控件引用为



You should look into using JQuery. You would reference the control as


("[id


=''add_acad'']")
=''add_acad'']")



这将找到任何ID包含"add_acad"的控件,无论它在页面上的什么位置或前缀是什么.



This will find any control with an id that contains "add_acad" no matter where on the page it is or what the prefix is.


这篇关于添加runat属性时,aspx页面的javascript中无法访问html复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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