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

html - Javascript on a checkbox is not working - Stack Overflow

programmeradmin8浏览0评论

I have the following trivial page:

<html>  
<head>  
<script type="text/javascript">  
    function check(){  
        var cb = document.getElementById('aaa');  
        if(!cb.checked()){  
            alert("test!!!");  
        }  
    }  
</script>  
</head>  
<body>  
<input id="aaa" type="checkbox" name="modify" onclick="check()">Modify</input>  
</body>  
</html>  

If I press the check box I see the following error in the javascript console:

Uncaught TypeError: Property 'checked' of object #<HTMLInputElement> is not a function  
check   
onclick    

I also tried with documents.getElementsByName('modify') and the same problem.
What am I doing wrong here?

I have the following trivial page:

<html>  
<head>  
<script type="text/javascript">  
    function check(){  
        var cb = document.getElementById('aaa');  
        if(!cb.checked()){  
            alert("test!!!");  
        }  
    }  
</script>  
</head>  
<body>  
<input id="aaa" type="checkbox" name="modify" onclick="check()">Modify</input>  
</body>  
</html>  

If I press the check box I see the following error in the javascript console:

Uncaught TypeError: Property 'checked' of object #<HTMLInputElement> is not a function  
check   
onclick    

I also tried with documents.getElementsByName('modify') and the same problem.
What am I doing wrong here?

Share Improve this question asked May 26, 2013 at 16:59 JimJim 19.6k41 gold badges146 silver badges266 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

checked isn't a function, as the error message states.

Use

if(!cb.checked){  

(without the parenthesis)

Note that you don't have to look for the element using getElementById, you can pass the element :

<input id="aaa" type="checkbox" name="modify" onclick="check(this)">Modify  

function check(cb){  
    if(!cb.checked()){  
        alert("test!!!");  
    }  
} 

Note also that the input element should not be closed. What you probably need here is the label element, which lets you click on the text to (un)check the box :

 <input id="aaa" type="checkbox" name="modify" onclick="check(this)">
 <label for=aaa>Modify</label> 

remove the brackets from cb.checked() - it is not a function. This code should work:

<html>  
<head>  
<script type="text/javascript">  
    function check(){  
        var cb = document.getElementById('aaa');  
        if(!cb.checked){  
            alert("test!!!");  
        }  
    }  
</script>  
</head>  
<body>  
<input id="aaa" type="checkbox" name="modify" onclick="check()">Modify</input>  
</body>  
</html>  

You can try with jquery also and you wrongly added end tag for input type

<script type="text/javascript">
    $(document).ready(function(){
    $('#aaa').click (function ()
    {
    if ($(this).is(':checked'))
    {
    // Do stuff
    alert("test!!!");
    }
    });
    });
    </script>
    <input id="aaa" type="checkbox" name="modify"  /> 
发布评论

评论列表(0)

  1. 暂无评论