最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Check if Checkbox is checkd? - Stack Overflow

matteradmin3PV0评论

Im trying to get some checkbox with a specific name

document.getElementsByName("test");

Unfortunatley i cant check if it is checked or not here is the code

for(i=0;i<check.length;i++)
        {
            if(check[i].checked==true)
            {
                alert(check[i].value);
            }
        }

Is somewhere a typo?

Im trying to get some checkbox with a specific name

document.getElementsByName("test");

Unfortunatley i cant check if it is checked or not here is the code

for(i=0;i<check.length;i++)
        {
            if(check[i].checked==true)
            {
                alert(check[i].value);
            }
        }

Is somewhere a typo?

Share Improve this question asked Mar 25, 2010 at 12:03 streetparadestreetparade 33k39 gold badges105 silver badges123 bronze badges 2
  • 2 Maybe you’re not selecting the right elements. – Gumbo Commented Mar 25, 2010 at 12:06
  • No i dont get any errors – streetparade Commented Mar 25, 2010 at 12:10
Add a ment  | 

3 Answers 3

Reset to default 3

jQuery would be nice for these basic things.

But you aren't using jQuery so:

var check = document.getElementsByName("test");

instead of just

document.getElementsByName("test");

Also, you can remove ==true, so you get:

if(check[i].checked)

Which makes much cleaner code.

Also, are you sure you set the name of the checkboxes to "test" (sometimes people forget these things, like me every time ^^)

jQuery Example

First, download jQuery from http://jquery./

$("input[type=checkbox][name=test]:checked").each(function() {
    alert($(this).val());
});

That should do it. If you aren't familar with jQuery, look at this: http://docs.jquery./Tutorials:How_jQuery_Works

Use the following code

<html>
<script type="text/javascript">
function test()
{

if(document.getElementById("chk").checked)
{

  alert('Checked');
}
}
</script>
<body>
<input type="checkbox" id="chk">
<input type="button" onclick="test();"></input>
</input>
</body>
</html>
for(i=0;i<check.length;i++)
        {
            if(check[i].checked==1)
            {
                alert(check[i].value);
            }
        }

Try this ?

@streetparade : tell me if this is also not working .... so that i can delete my answer... people didn't like it

Post a comment

comment list (0)

  1. No comments so far