1
function piliang() {
    var ids = "";
    var num = document.getElementsByName("check");
    for (var i = 0; 1 < num.length; i++) {
        if (num.item(i).checked) {
            ids = ids + num.item(i).value + ",";
        }
        alert(ids);
    }

run successfully

function piliang() {
    var ids = "";
    var num = document.getElementsByName("check");
    for (var i = 0; 1 < num.length; i++) {
        if (num.item(i).checked) {
            ids = ids + num.item(i).value + ",";
        }
    }

This gives out the following error:

"Cannot read property 'checked' of null"

The first code also has this error, but it can run successfully

My English is not good, please try to explain it in code, thank you very much

2
  • 2
    Both codes look identical so that should not be the case. Could you provide some more info. A good idea will be to print out the value using console.log(num.item(i).checked) Commented Nov 9, 2017 at 6:08
  • Thank you. That's settled @BabyGroot Commented Nov 9, 2017 at 12:32

1 Answer 1

1

Fixed the for loop

function piliang() {
    var ids = "";
    var num = document.getElementsByName("check");
    for (var i = 0; 1 < num.length; i++) {    //use i instead of 1
        if (num.item(i).checked) {
            ids = ids + num.item(i).value + ",";
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Just a pointer, this questions qualifies as a typographical mistake. There is a close option for such posts. So its a bad practice to answer such question. You can comment solution under question and if you have enough rep, then even post a close vote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.