最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Checkbox "Error: Unable to get value of the property 'checked': object is null or undefine

programmeradmin13浏览0评论

I'm trying to resolve whether or not a checkbox is checked. It seems rather straight forward.

<script type="text/Javascript">
function ValidateReqNum() {

        var zCheckBox = document.getElementById('chkAllJobs');

        if (zCheckBox.checked)
            alert("true");

        if (!zCheckBox.checked)
            alert("false");

        return true;
    }
</script>

and the checkbox:

 <asp:CheckBox ID="chkAllJobs" runat="server" Text="All Jobs" />

called from:

<asp:Button ID="btnPrintReport" runat="server" Text="Run Report" 
  OnClientClick="return ValidateReqNum();" OnClick="CreatePDFJobReport" />

I've tried it dozens of different ways and it keeps ing back with

Error: Unable to get value of the property 'checked': object is null or undefined

My other elements in the same aspx page are reporting in just fine. I can call chkAllJobs from my c# code and I can resolve whether or not it's checked from c# as well.

I'm trying to resolve whether or not a checkbox is checked. It seems rather straight forward.

<script type="text/Javascript">
function ValidateReqNum() {

        var zCheckBox = document.getElementById('chkAllJobs');

        if (zCheckBox.checked)
            alert("true");

        if (!zCheckBox.checked)
            alert("false");

        return true;
    }
</script>

and the checkbox:

 <asp:CheckBox ID="chkAllJobs" runat="server" Text="All Jobs" />

called from:

<asp:Button ID="btnPrintReport" runat="server" Text="Run Report" 
  OnClientClick="return ValidateReqNum();" OnClick="CreatePDFJobReport" />

I've tried it dozens of different ways and it keeps ing back with

Error: Unable to get value of the property 'checked': object is null or undefined

My other elements in the same aspx page are reporting in just fine. I can call chkAllJobs from my c# code and I can resolve whether or not it's checked from c# as well.

Share Improve this question edited Jan 4, 2013 at 0:23 Kaf 33.8k7 gold badges60 silver badges80 bronze badges asked Jan 3, 2013 at 23:35 user1947046user1947046 431 silver badge3 bronze badges 1
  • 1 your error message is telling you that zCheckBox is undefined, look at @Kaf's answer, that should go the trick – roman m Commented Jan 4, 2013 at 0:30
Add a ment  | 

2 Answers 2

Reset to default 3

If you are using master pages, control ids of child page at client will be different to their server ids. So instead of using server control name, try using its client id as;

var zCheckBox = document.getElementById('<%= chkAllJobs.ClientID %>');

function ValidateReqNum() {
   alert(zCheckBox.checked);
}

Here is a sample how check-box work. If you have the following check-box:

 <input id="Checkbox" type="checkbox" name="mycheckbox" value="5"/>

Then you can get the value using forms collection

label_Result.text = Request.Form["mycheckbox"]; 

Consequently, you will get the value 5 only if that checkbox is checked.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论